filename
stringlengths
7
140
content
stringlengths
0
76.7M
code/online_challenges/src/project_euler/problem_003/problem_003.py
def main(): n = 600851475143 h = 0 c = 2 while n != 1: if n % c == 0 and c > h: h = c n /= c c += 1 print(h) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_004/README.md
# Project Euler Problem #004: Largest palindrome product ([Problem Link](https://projecteuler.net/problem=4)) A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 Γ— 99. Find the largest palindrome made from the product of two 3-digit numbers....
code/online_challenges/src/project_euler/problem_004/problem_004.cpp
#include <iostream> #include <string> int main() { int l = 0; for (int i = 100; i <= 999; ++i) for (int j = 100; j <= 999; ++j) { int c = i * j; std::string cS = std::to_string(c); if ((cS == std::string{ cS.rbegin(), cS.rend() }) && (c > l)) ...
code/online_challenges/src/project_euler/problem_004/problem_004.java
public class problem_004 { public static void main( String[] args) { int lar = 0; // Nested loop to iterate for every three digit number for(int i = 100; i <= 999; ++i) { for(int j = 100; ...
code/online_challenges/src/project_euler/problem_004/problem_004.py
def main(): num = 0 for i in range(100, 1000): for j in range(100, 1000): c = i * j cS = str(c) if cS == cS[::-1] and c > num: num = c print(num) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_005/README.md
# Project Euler Problem #005: Smallest multiple ([Problem Link](https://projecteuler.net/problem=5)) 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? --- <p al...
code/online_challenges/src/project_euler/problem_005/problem_005.c
#include <stdio.h> int main(void) { int d = 1; while (!( (d % 11 == 0) && (d % 12 == 0) && (d % 13 == 0) && (d % 14 == 0) && (d % 15 == 0) && (d % 16 == 0) && (d % 17 == 0) && (d % 18 == 0) && (d % 19 == 0) && (d % 20 == 0) ...
code/online_challenges/src/project_euler/problem_005/problem_005.cpp
#include <iostream> int main() { int d = 1; while (!( (d % 11 == 0) && (d % 12 == 0) && (d % 13 == 0) && (d % 14 == 0) && (d % 15 == 0) && (d % 16 == 0) && (d % 17 == 0) && (d % 18 == 0) && ...
code/online_challenges/src/project_euler/problem_005/problem_005.java
public class Problem005 { public static boolean isDivisible(int number) { for(int i = 1; i <= 20; ++i) { if(number % i != 0) return false; } return true; } public static void main(String []args) { int number = 1; while(!isDivisible(number)) { ++number; } System.out.println(number); } }
code/online_challenges/src/project_euler/problem_005/problem_005.py
def gcd(n1, n2): remainder = 1 while remainder != 0: remainder = n1 % n2 n1 = n2 n2 = remainder return n1 def lcm(n1, n2): return (n1 * n2) / gcd(n1, n2) def main(): num = 1 for i in range(1, 21): num = lcm(num, i) print(num) if __name__ == "__main__": ...
code/online_challenges/src/project_euler/problem_006/README.md
# Project Euler Problem #006: Sum square difference ([Problem Link](https://projecteuler.net/problem=6)) The sum of the squares of the first ten natural numbers is, <p align="center"> 1<sup>2</sup> + 2<sup>2</sup> + ... + 10<sup>2</sup> = 385 </p> The square of the sum of the first ten natural numbers is, <p align=...
code/online_challenges/src/project_euler/problem_006/problem_006.cpp
#include <iostream> int main() { long long int sumOfSquares = 0LL; long int squareOfSum = 0LL; for (int n = 1; n <= 100; ++n) { sumOfSquares += (n * n); squareOfSum += n; } squareOfSum *= squareOfSum; std::cout << (squareOfSum - sumOfSquares) << "\n"; }
code/online_challenges/src/project_euler/problem_006/problem_006.java
import java.util.*; import java.lang.*; import java.io.*; class Problem006{ public static void main(String[] args) { int sum = 0; int sqsum = 0; for (int i = 1; i <= 100; i++) { sqsum += i * i; sum += i; } System.out.println(sum * sum - sqsum); } }
code/online_challenges/src/project_euler/problem_006/problem_006.py
def main(): sum_of_squares = 0 square_of_sum = 0 for n in range(1, 101): sum_of_squares += n * n square_of_sum += n square_of_sum *= square_of_sum print(square_of_sum - sum_of_squares) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_007/README.md
# Project Euler Problem #007: 10001st prime ([Problem Link](https://projecteuler.net/problem=7)) By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? --- <p align="center"> A massive collaborative effort by <a href="https://github....
code/online_challenges/src/project_euler/problem_007/problem_007.cpp
#include <cmath> #include <iostream> #include <vector> std::vector<long long int> primesUpto(size_t limit) // Function that implements the Sieve of Eratosthenes { std::vector<bool> primesBoolArray(limit, true); std::vector<long long int> primesUptoLimit; primesBoolArray[0] = primesBoolArray[1] = false; ...
code/online_challenges/src/project_euler/problem_007/problem_007.js
/* Part of Cosmos by OpenGenus Foundation */ const primes = []; let n = 2; while (primes.length < 10001) { // if this number is not divisible by any prime currently in the array if (primes.reduce((isPrime, prime) => isPrime && n % prime !== 0, true)) { primes.push(n); } n++; } console.log(primes[10000])...
code/online_challenges/src/project_euler/problem_007/problem_007.py
def main(): n = 10001 x = 2 list_of_primes = [] while len(list_of_primes) < n: if all(x % prime for prime in list_of_primes): list_of_primes.append(x) x += 1 print(list_of_primes[-1]) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_008/README.md
# Project Euler Problem #008: Largest product in a series ([Problem Link](https://projecteuler.net/problem=8)) The four adjacent digits in the 1000-digit number that have the greatest product are 9 Γ— 9 Γ— 8 Γ— 9 = 5832. <p align="center"> 73167176531330624919225119674426574742355349194934 96983520312774506326239578318...
code/online_challenges/src/project_euler/problem_008/problem_008.java
// Part of Cosmos by OpenGenus import java.math.BigInteger; import java.util.ArrayList; import java.util.Scanner; public class Solution { public static void main(String[] args) { // TODO Auto-generated method stub String num="731671765313306249192251196744265747423553491949349698352031277450632623957...
code/online_challenges/src/project_euler/problem_008/problem_008.py
def main(): numbers = "73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664...
code/online_challenges/src/project_euler/problem_009/README.md
# Project Euler Problem #009: Special Pythagorean triplet ([Problem Link](https://projecteuler.net/problem=9)) A Pythagorean triplet is a set of three natural numbers, _a_ < _b_ < _c_, for which, _a_<sup>2</sup> + _b_<sup>2</sup> = _c_<sup>2</sup> For example, 3<sup>2</sup> + 4<sup>2</sup> = 9 + 16 = 25 = 5<sup>2</s...
code/online_challenges/src/project_euler/problem_009/problem_009.cpp
#include <iostream> int main() { for (int i = 1; i < 1000; ++i) for (int j = 1; j < 1000; ++j) { for (int k = 1; k < 1000; ++k) if (((i * i) + (j * j) == (k * k)) && ((i + j + k) == 1000)) { std::cout << i * j * k << "\n"; ...
code/online_challenges/src/project_euler/problem_009/problem_009.java
// Part of Cosmos by OpenGenus public class Solution{ public static void main(String[] args) { // TODO Auto-generated method stub long largest=0l; int flag=0; long sum=1000; for(long a=1;a<sum/3;a++) { long asq=a*a; long b=((a*a)-(a-sum)*(a-sum))/(2*(a-sum)); lo...
code/online_challenges/src/project_euler/problem_009/problem_009.py
def main(): sum_total = 1000 for c in range(sum_total): for b in range(c): for a in range(b): if (a + b + c == sum_total) and (a ** 2 + b ** 2 == c ** 2): print(a * b * c) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_010/README.md
# Project Euler Problem #010: Summation of primes ([Problem Link](https://projecteuler.net/problem=10)) The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. --- <p align="center"> A massive collaborative effort by <a href="https://github.com/OpenGenus/cosmos">Op...
code/online_challenges/src/project_euler/problem_010/problem_010.cpp
#include <cmath> #include <iostream> #include <vector> long long int sumOfPrimesUpto(size_t limit) // Function that implements the Sieve of Eratosthenes { std::vector<bool> primesBoolArray(limit, true); long long int sum = 0; primesBoolArray[0] = primesBoolArray[1] = false; for (size_t i = 2; i < limit...
code/online_challenges/src/project_euler/problem_010/problem_010.java
// Part of Cosmos by OpenGenus public class Solution { static boolean checkp(int x) { if(x==0 || x==1) return false; if (x==2) { return true; } if(x%2==0) { return false; } else { for (int i...
code/online_challenges/src/project_euler/problem_010/problem_010.py
def main(): n = 2000000 sum = 0 prime = [True] * n for p in range(2, n): if prime[p]: sum += p for i in range(p * p, n, p): prime[i] = False print(sum) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_011/README.md
# Project Euler Problem #011: Largest product in a grid ([Problem Link](https://projecteuler.net/problem=11)) In the 20Γ—20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81...
code/online_challenges/src/project_euler/problem_011/problem_011.cpp
#include <bits/stdc++.h> using namespace std; int main() { int n = 20; int a[n][n]; // Passing the 20 * 20 array as input for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { cin >> a[i][j]; } } int dr[8] = {1, 1, 0, -1, -1, -1, 0, 1}; int dc[8] ...
code/online_challenges/src/project_euler/problem_012/README.md
# Project Euler Problem #012: Highly divisible triangular number ([Problem Link](https://projecteuler.net/problem=12)) The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28...
code/online_challenges/src/project_euler/problem_012/problem_012.cpp
#include <iostream> #include <cmath> int main() { int divisorCount = 0; int triangleNumberIndex = 0; int triangleNumber = 0; while (divisorCount < 500) { divisorCount = 0; ++triangleNumberIndex; triangleNumber += triangleNumberIndex; for (int i = 1; i < std::sqrt(tr...
code/online_challenges/src/project_euler/problem_012/problem_012.py
import math def main(): i = 1 triangle_number = 0 divisor_count = 0 while divisor_count < 500: triangle_number += i i += 1 divisor_count = 0 for z in range(1, int(math.sqrt(triangle_number))): if triangle_number % z == 0: divisor_count += 2...
code/online_challenges/src/project_euler/problem_013/README.md
# Project Euler Problem #13: Large Sum ([Problem Link](https://projecteuler.net/problem=13)) Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. ``` 37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 743249861995247410594742333095...
code/online_challenges/src/project_euler/problem_013/problem_013.py
def main(): numbers = [ 37107287533902102798797998220837590246510135740250, 46376937677490009712648124896970078050417018260538, 74324986199524741059474233309513058123726617309629, 91942213363574161572522430563301811072406154908250, 23067588207539346171171980310421047513778063...
code/online_challenges/src/project_euler/problem_014/README.md
# Project Euler Problem #014: Longest Collatz sequence ([Problem Link](https://projecteuler.net/problem=14)) The following iterative sequence is defined for the set of positive integers: _n_ β†’ _n_/2 (_n_ is even) _n_ β†’ 3 _n_ + 1 (_n_ is odd) Using the rule above and starting with 13, we generate the following seque...
code/online_challenges/src/project_euler/problem_014/problem_014.cpp
#include <iostream> long long int collatzSequenceSize(long long int n) { long long int result = 0; while (n != 1) { n = (n % 2 == 0) ? n / 2 : n * 3 + 1; ++result; } return result; } int main() { long long int l = 0; long long int lSize = 0; for (long long int i = 1; i ...
code/online_challenges/src/project_euler/problem_014/problem_014.py
def main(): dic = {n: 0 for n in range(1, 1000000)} for n in range(3, 1000000, 1): count = 0 number = n while True: if n < number: dic[number] = dic[n] + count break if n % 2 == 0: n = n / 2 count +...
code/online_challenges/src/project_euler/problem_016/README.md
# Project Euler Problem #016: Power digit sum ([Problem Link](https://projecteuler.net/problem=16)) 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 2^1000? --- <p align="center"> A massive collaborative effort by <a href="https://github.com/OpenGenus/c...
code/online_challenges/src/project_euler/problem_016/problem_016.py
def main(): n = 2 ** 1000 s = list(str(n)) ans = 0 for i in s: ans += int(i) print(ans) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_017/README.md
# Project Euler Problem #17: Number letter counts ([Problem Link](https://projecteuler.net/problem=17)) If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out...
code/online_challenges/src/project_euler/problem_017/problem_017.cpp
#include <bits/stdc++.h> using namespace std; int num_letters_func(int n) { int num_letters[91]; num_letters[1] = 3; num_letters[2] = 3; num_letters[3] = 5; num_letters[4] = 4; num_letters[5] = 4; num_letters[6] = 3; num_letters[7] = 5; num_letters[8] = 5; num_letters[9] = 4; ...
code/online_challenges/src/project_euler/problem_018/README.md
# Project Euler Problem #18: Maximum path sum I ([Problem Link](https://projecteuler.net/problem=18)) By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. <p align="center"> <b>3</b> <b>7</b> 4 2 <b>4</b> 6 ...
code/online_challenges/src/project_euler/problem_018/problem_018.py
def main(): prob = [ [75], [95, 64], [17, 47, 82], [18, 35, 87, 10], [20, 4, 82, 47, 65], [19, 1, 23, 75, 3, 34], [88, 2, 77, 73, 7, 63, 67], [99, 65, 4, 28, 6, 16, 70, 92], [41, 41, 26, 56, 83, 40, 80, 70, 33], [41, 48, 72, 33, 47, 32,...
code/online_challenges/src/project_euler/problem_019/problem_019.java
import java.util.HashMap; import java.util.Map; class Problem019 { public static void main(String args[]){ Map<Integer, String> L = new HashMap<Integer, String>(); L.put(1, "Sun"); L.put(2, "Mon"); L.put(3, "Tue"); L.put(4, "Wed"); L.put(5, "Thurs"); L.put(6,...
code/online_challenges/src/project_euler/problem_020/README.md
# Project Euler Problem 20: Factorial digit sum ([Problem Link](https://projecteuler.net/problem=20)) n! means n Γ— (n βˆ’ 1) Γ— ... Γ— 3 Γ— 2 Γ— 1 For example, 10! = 10 Γ— 9 Γ— ... Γ— 3 Γ— 2 Γ— 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number ...
code/online_challenges/src/project_euler/problem_020/problem_020.java
import java.math.BigInteger; public class Problem_020 { public static BigInteger factorial(BigInteger number) { if (number.equals(BigInteger.ZERO)) return BigInteger.ONE; return (number.multiply(factorial(number.subtract(BigInteger.ONE)))); } public static BigInteger addDigits(BigInteger n) { BigInteger su...
code/online_challenges/src/project_euler/problem_020/problem_020.py
def main(): factorial = 1 for i in range(100): factorial *= i + 1 digit_sum = 0 while factorial > 0: digit_sum += factorial % 10 factorial = factorial // 10 print(digit_sum) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_021/README.md
# Project Euler Problem #021: Amicable numbers ([Problem Link](https://projecteuler.net/problem=21)) Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a β‰  b, then a and b are an amicable pair and each of a and b are called amicable...
code/online_challenges/src/project_euler/problem_021/problem_021.cpp
#include <iostream> int sumProperDivisors(int n) { int sum = 0; for (int i = 1; i * i <= n; ++i) if (n % i == 0) { sum += i; if (n / i != i) sum += n / i; } return sum - n; } bool isAmicableNumber(int n) { int m = sumProperDivisors(n); ...
code/online_challenges/src/project_euler/problem_022/README.md
# Project Euler Problem #022: Names scores ([Problem Link](https://projecteuler.net/problem=22)) Using ([names.txt](https://projecteuler.net/project/resources/p022_names.txt))(right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical or...
code/online_challenges/src/project_euler/problem_022/problem_022.py
def main(): names = [ "MARY", "PATRICIA", "LINDA", "BARBARA", "ELIZABETH", "JENNIFER", "MARIA", "SUSAN", "MARGARET", "DOROTHY", "LISA", "NANCY", "KAREN", "BETTY", "HELEN", "SANDRA", ...
code/online_challenges/src/project_euler/problem_023/README.md
# Project Euler Problem #023: Non-abundant sums ([Problem Link](https://projecteuler.net/problem=23)) A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfe...
code/online_challenges/src/project_euler/problem_023/problem_023.cpp
#include <bits/stdc++.h> using namespace std; int isAbundant(int x) { int sum = 0; for(int i = 1; i * i <= x; i++) { if(x % i == 0) { if((x / i) == i) { sum += i; } else { sum += i + (x / i); ...
code/online_challenges/src/project_euler/problem_024/README.md
# Project Euler Problem #024: Lexicographic permutations ([Problem Link](https://projecteuler.net/problem=24)) A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it ...
code/online_challenges/src/project_euler/problem_024/problem_024.py
from itertools import permutations def main(): result = list(map("".join, permutations("0123456789"))) print(result[999999]) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_025/README.md
# Project Euler Problem #025: 1000-digit Fibonacci number ([Problem Link](https://projecteuler.net/problem=25)) The Fibonacci sequence is defined by the recurrence relation: <p align="center"> F<sub>n</sub> = F<sub>nβˆ’1</sub> + F<sub>nβˆ’2</sub>, where F<sub>1</sub> = 1 and F<sub>2</sub> = 1. </p> Hence the first 12 t...
code/online_challenges/src/project_euler/problem_025/problem_025.cpp
#include <iostream> #include <vector> int main() { std::vector<int> prevFibonacci, currFibonacci; prevFibonacci.reserve(1000); currFibonacci.reserve(1000); int count = 2; prevFibonacci.push_back(1); currFibonacci.push_back(1); while (currFibonacci.size() < 1000) { std::vector<i...
code/online_challenges/src/project_euler/problem_025/problem_025.py
def main(size): last, actual = 1, 1 index = 2 while actual < size: last, actual = actual, last + actual index += 1 print(index) if __name__ == "__main__": main(10 ** 999)
code/online_challenges/src/project_euler/problem_026/README.md
# Project Euler Problem #026: Reciprocal cycles ([Problem Link](https://projecteuler.net/problem=26)) A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: 1/2 = 0.5 1/3 = 0.(3) 1/4 = 0.25 1/5 = 0.2...
code/online_challenges/src/project_euler/problem_026/problem_026.cpp
#include <iostream> #include <vector> int main() { int remainder, value, position, sequenceLength = 0; for (int i = 1000; i > 0; --i) { std::vector<int> cycleCheckArray; for (int j = 0; j < i; ++j) cycleCheckArray.push_back(0); remainder = 1, value = 1, position = 0; ...
code/online_challenges/src/project_euler/problem_027/README.md
# Project Euler Problem #027: Quadratic primes ([Problem Link](https://projecteuler.net/problem=27)) Euler discovered the remarkable quadratic formula: <div align="center">n ^ 2 + n + 41</div> It turns out that the formula will produce 40 primes for the consecutive integer values 0 <= n <= 39. However, when n = 40...
code/online_challenges/src/project_euler/problem_027/problem_027.cpp
#include <bits/stdc++.h> using namespace std; bool isPrime(int n) { bool ans = true; for(int i = 2; i * i <= n; i++) { if(n % i == 0) { ans = false; break; } } return ans; } int main() { int max_primes = 0; int prod; for(int a = -999; a <...
code/online_challenges/src/project_euler/problem_028/README.md
# Project Euler Problem #028: Number spiral diagonals ([Problem Link](https://projecteuler.net/problem=28)) Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: <p align="center"> <pre> 21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 1...
code/online_challenges/src/project_euler/problem_028/problem_028.cpp
#include <iostream> #include <cmath> bool isPerfectSquare(int num) { int sqrtNum = static_cast<int>(std::sqrt(num)); return sqrtNum * sqrtNum == num; } int main() { int limit = 1001 * 1001; int incrementRate = 0; int diagonalNumberSum = 0; for (int i = 1; i <= limit; i += incrementRate) // Ite...
code/online_challenges/src/project_euler/problem_028/problem_028.py
from math import sqrt def is_perfect_square(integer): sqrt_num = int(sqrt(integer)) return sqrt_num * sqrt_num == integer def main(): limit = 1001 * 1001 increment_rate = 0 diagonal_number_sum = 0 i = 1 while i <= limit: diagonal_number_sum += i if i % 2 == 1 and is_perf...
code/online_challenges/src/project_euler/problem_034/README.md
# Project Euler Problem #034: Digit factorials ([Problem Link](https://projecteuler.net/problem=34)) 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not includ...
code/online_challenges/src/project_euler/problem_034/problem_034.cpp
#include <iostream> #include <vector> int factorial (std::size_t n) { int fact = 1; for (std::size_t i = 1; i <= n; ++i) fact *= i; return fact; } int main() { std::vector<int> factorials(10); constexpr std::size_t maxDigitFactorial = 2540162; for (int i = 0; i < 10; ++i) facto...
code/online_challenges/src/project_euler/problem_036/README.md
# Project Euler Problem #036: Double-base palindromes ([Problem Link](https://projecteuler.net/problem=36)) The decimal number, 585 = 10010010012 (binary), is palindromic in both bases. Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. --- <p align="center"> A massiv...
code/online_challenges/src/project_euler/problem_036/problem_036.cpp
#include <bitset> #include <iostream> #include <string> bool isPalindrome(const std::string& str); int main() { int sum = 0; for (int i = 1; i < 1000000; ++i) if (isPalindrome(std::to_string(i))) { std::string currentBinaryString = std::bitset<32>(i).to_string(); curre...
code/online_challenges/src/project_euler/problem_036/problem_036.py
def base_check(a, y): ans = "" while a > 0: ans += str(a % y) a = a // y return ans == ans[::-1] def palindrome(x, base): x = str(x) if x != x[::-1]: return False else: x = int(x) x = base_check(x, base) x = str(x) return x == x[::-1] n...
code/online_challenges/src/project_euler/problem_037/README.md
# Project Euler Problem #037: Truncatable Primes ([Problem Link](https://projecteuler.net/problem=37)) The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from...
code/online_challenges/src/project_euler/problem_037/problem_037.cpp
#include <array> #include <cmath> #include <iostream> template<std::size_t N> std::array<bool, N> primesUpto() // Function that implements the Sieve of Eratosthenes { std::array<bool, N> primesList; std::fill(primesList.begin(), primesList.end(), true); primesList[0] = primesList[1] = false; std::si...
code/online_challenges/src/project_euler/problem_040/README.md
# Project Euler Problem #040: Champernowne's constant ([Problem Link](https://projecteuler.net/problem=40)) An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the ...
code/online_challenges/src/project_euler/problem_040/problem_040.py
def main(): nums = "" # The string containing the sequence of numbers i = 0 # Used for iteration while len(nums) < 1000002: # Loop to add the sequence of numbers to the list nums += str(i) i += 1 answer = 1 # Initialising answer which will store final computed answer i = 1 # R...
code/online_challenges/src/project_euler/problem_067/README.md
# Project Euler Problem #067: Maximum path sum II ([Problem Link](https://projecteuler.net/problem=67)) By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. ``` 3 7 4 2 4 6 8 5 9 3 ``` That is, 3 + 7 + 4 + 9 = 23. Find ...
code/online_challenges/src/project_euler/problem_067/problem_067.py
def main(): prob = [ [59], [73, 41], [52, 40, 9], [26, 53, 6, 34], [10, 51, 87, 86, 81], [61, 95, 66, 57, 25, 68], [90, 81, 80, 38, 92, 67, 73], [30, 28, 51, 76, 81, 18, 75, 44], [84, 14, 95, 87, 62, 81, 17, 78, 58], [21, 46, 71, 58, 2,...
code/online_challenges/src/project_euler/problem_102/README.md
# Project Euler Problem #102: Triangle containment ([Problem Link](https://projecteuler.net/problem=102)) Three distinct points are plotted at random on a Cartesian plane, for which -1000 ≀ x, y ≀ 1000, such that a triangle is formed. Consider the following two triangles: <p align="center"> A(-340,495), B(-153,-910...
code/online_challenges/src/project_euler/problem_102/problem_102.cpp
#include <cmath> #include <iostream> #include <fstream> struct Coord { int x; int y; }; int doubleTriangleArea(Coord a, Coord b, Coord c) // Area doesn't actually need to be calculated either, // just compared for equality { /* * Coordinate area metho...
code/online_challenges/src/project_euler/problem_102/triangles.txt
-340,495,-153,-910,835,-947 -175,41,-421,-714,574,-645 -547,712,-352,579,951,-786 419,-864,-83,650,-399,171 -429,-89,-357,-930,296,-29 -734,-702,823,-745,-684,-62 -971,762,925,-776,-663,-157 162,570,628,485,-807,-896 641,91,-65,700,887,759 215,-496,46,-931,422,-30 -119,359,668,-609,-358,-494 440,929,968,214,760,-857 -7...
code/online_challenges/src/rosalind/README.md
# Cosmos Rosalind > Solutions to Rosalind bioinformatics challenges. http://rosalind.info/problems/list-view/ --- <p align="center"> A massive collaborative effort by <a href="https://github.com/OpenGenus/cosmos">OpenGenus Foundation</a> </p> ---
code/online_challenges/src/rosalind/complement_dna_strand/complement_dna.rs
fn complement_dna(dna: &str) -> String { dna.chars() .rev() .map(|c| match c { 'A' => 'T', 'T' => 'A', 'C' => 'G', 'G' => 'C', _ => ' ', }) .collect() } #[cfg(test)] mod tests { use super::*; #[test] fn sample_t...
code/online_challenges/src/rosalind/complement_dna_strand/complement_dna_strand.exs
defmodule ComplementDnaStrand do def complement(strand) do strand = strand |> String.reverse |> String.codepoints Enum.map(strand, fn nucleotide -> case nucleotide do "A" -> "T" "T" -> "A" "G" -> "C" "C" -> "G" end end) |> List.to_string end end IO.put...
code/online_challenges/test/README.md
# cosmos Your personal library of every algorithm and data structure code that you will ever encounter
code/operating_system/src/README.md
# Operating Systems Operating Systems are software programs that communicate with the hardware and other application programs. They are responsible for a number of functions such as resource allocation in multi-user systems, management of files and processes (a 'process' is used to refer to a program in execution), han...
code/operating_system/src/concurrency/dining_philosophers/README.md
# Dining philosophers problem Dining philosophers problem is an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them. ## Problem statement Consider there are five philosophers sitting around a circular dining table. The dining table has five c...
code/operating_system/src/concurrency/dining_philosophers/dining_philosophers.c
/* Dining Philosophers Problem*/ #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> void *func(int n); pthread_t philosopher[5]; pthread_mutex_t chopstick[5]; int main() { int i, k; void *msg; for (i = 1; i <= 5; i++) { k = pthread_mutex_init(&chopstick[i], NULL); if (k == -1) ...
code/operating_system/src/concurrency/monitors/monitors_system_v/main.c
/* * Part of Cosmos by OpenGenus Foundation. * Implementation of Hoare monitors using System V IPC. * An example of how to use monitors. * Author : ABDOUS Kamel */ #include "monitors.h" #include <stdio.h> #include <unistd.h> #include <sys/wait.h> #include <string.h> #include <errno.h> #define NB_CONDS 1 typed...
code/operating_system/src/concurrency/monitors/monitors_system_v/monitors.c
/* * Part of Cosmos by OpenGenus Foundation. * Implementation of Hoare monitors using System V IPC. * Author : ABDOUS Kamel */ #include "monitors.h" static struct sembuf mutex_up = {MTOR_MUTEX, 1, 0}, mutex_down = {MTOR_MUTEX, -1, 0}, sig_up = {MTOR_SIG_SEM, 1, 0}, ...
code/operating_system/src/concurrency/monitors/monitors_system_v/monitors.h
/* * Part of Cosmos by OpenGenus Foundation. * Implementation of Hoare monitors using System V IPC. * Author : ABDOUS Kamel */ #ifndef MONITORS_H #define MONITORS_H #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/sem.h> #include <sys/stat...
code/operating_system/src/concurrency/peterson_algorithm_for_mutual_exclusion/peterson_algorithm_in_c/mythreads.h
// mythread.h (A wrapper header file with assert // statements) #ifndef __MYTHREADS_h__ #define __MYTHREADS_h__ #include <pthread.h> #include <assert.h> #include <sched.h> void Pthread_mutex_lock(pthread_mutex_t *m) { int rc = pthread_mutex_lock(m); assert(rc == 0); } void Pthread_mutex_unlock(pthread_mutex_...
code/operating_system/src/concurrency/peterson_algorithm_for_mutual_exclusion/peterson_algorithm_in_c/peterson_algo_mutual_exclusion_in_c.c
// Peterson's algorithm // C implementation // Here mythreads.h is a file externally included by me with some assertion statements #include <stdio.h> #include <pthread.h> #include"mythreads.h" int flag[2]; int turn; const int MAX = 1e9; int ans = 0; void lock_init() { // Initialize lock by reseting the desire of ...
code/operating_system/src/concurrency/producer_consumer/producer_consumer.cpp
#include <iostream> #include <ctime> #include <cstdlib> #include <thread> #include <mutex> #include <vector> #include <condition_variable> const int N = 100; std::vector<int> shared_buffer(N); std::mutex lock_buffer; std::condition_variable cond; void producer() { while (true) { std::unique_lock<std:...
code/operating_system/src/concurrency/readers_writers/readers_writers.cpp
#include <iostream> #include <vector> #include <thread> #include <mutex> #include <chrono> #define N 5 // # of processes (readers & writes) std::mutex mu; // controlling access to the number of processes std::mutex db; // controlling acess to the database std::mutex print_mu; // guards the cout << I/O int rc = 0;...
code/operating_system/src/deadlocks/bankers_algorithm/README.md
# Banker's Algorithm ## Description Banker's algorithm is an conservative algorithm to check if a resource can be granted to a process if it is not used by any process in the system. It is used for deadlock management. ## Logic The algorithm relies on the following quantities: * *R(k)*: total amount of resource R<sub>k...
code/operating_system/src/deadlocks/bankers_algorithm/banker_safety.cpp
// Part of Cosmos by OpenGenus Foundation. // Banker's Algorithm: Safety Algorithm #include <cstdio> int main() { // Initialize int available[10], allocation [10][10], maximum[10][10]; int noOfProcesses, noOfResources, need[10][10]; int work[10], finish[10] = {0}, i; //Inputs printf("Enter no...
code/operating_system/src/memory_management/least_recently_used/lru.c
#include<stdio.h> int frame[3]={0,0,0},pref[3]={0,0,0},page[20]; int hita(int a) // counts hit { int n=1,i=0; for(i=0;i<3;i++) { if(frame[i]==a) { n=0; break; } else continue; }printf("%d",n); return n; } void initi(int a,int i) {int z=0,m...
code/operating_system/src/memory_management/least_recently_used/lru.cpp
/* Least Recently Used Page Replacement Algorithm implemented using a stack */ #include <bits/stdc++.h> using namespace std; /* A function that finds and returns the index of the current page in the page table If it is not present it would return -1 */ int find(int current_page,vector<int>& page_table){ for(in...
code/operating_system/src/memory_management/memory_mapping/mapping.c
#ifdef USE_MAP_ANON #define _BSD_SOURCE #endif #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/wait.h> #include <sys/mman.h> #include <fcntl.h> #include <unistd.h> int main(int argc, char *argv[]) { /*Point to previous shared memory*/ int *addr; #ifdef USE_MAP_ANON /*Using MAP_AN...