From f165876a2011297b58f417d671151e7e79464dce Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Thu, 19 Sep 2019 15:09:43 +0200 Subject: [PATCH] Add python solutions for first 10 problems --- C/p004.c | 2 +- Python/p001.py | 22 ++++++++++++++++ Python/p002.py | 30 +++++++++++++++++++++ Python/p003.py | 36 +++++++++++++++++++++++++ Python/p004.py | 39 +++++++++++++++++++++++++++ Python/p005.py | 22 ++++++++++++++++ Python/p006.py | 24 +++++++++++++++++ Python/p007.py | 26 ++++++++++++++++++ Python/p008.py | 50 +++++++++++++++++++++++++++++++++++ Python/p009.py | 32 ++++++++++++++++++++++ Python/p010.py | 24 +++++++++++++++++ Python/projecteuler.py | 60 ++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 366 insertions(+), 1 deletion(-) create mode 100644 Python/p001.py create mode 100644 Python/p002.py create mode 100644 Python/p003.py create mode 100644 Python/p004.py create mode 100644 Python/p005.py create mode 100644 Python/p006.py create mode 100644 Python/p007.py create mode 100644 Python/p008.py create mode 100644 Python/p009.py create mode 100644 Python/p010.py create mode 100644 Python/projecteuler.py diff --git a/C/p004.c b/C/p004.c index 7d31968..879e911 100644 --- a/C/p004.c +++ b/C/p004.c @@ -29,7 +29,7 @@ int main(int argc, char **argv) elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; printf("Project Euler, Problem 4\n"); - printf("Answer: %d \n", max); + printf("Answer: %d\n", max); printf("Elapsed time: %.9lf seconds\n", elapsed); diff --git a/Python/p001.py b/Python/p001.py new file mode 100644 index 0000000..12d2813 --- /dev/null +++ b/Python/p001.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +from timeit import default_timer + +def main(): + start = default_timer() + + sum_ = 0 + + for i in range(3, 1000): + if i % 3 == 0 or i % 5 == 0: + sum_ += i + + end = default_timer() + + print('Project Euler, Problem 1') + print('Answer: {}'.format(sum_)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p002.py b/Python/p002.py new file mode 100644 index 0000000..dafd087 --- /dev/null +++ b/Python/p002.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +from timeit import default_timer + +def main(): + + start = default_timer() + + fib1 = 1 + fib2 = 2 + fibn = fib1 + fib2 + sum_ = 2 + + while fibn < 4000000: + if fibn % 2 == 0: + sum_ = sum_ + fibn + + fib1 = fib2 + fib2 = fibn + fibn = fib1 + fib2 + + end = default_timer() + + print('Project Euler, Problem 2') + print('Answer: {}'.format(sum_)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p003.py b/Python/p003.py new file mode 100644 index 0000000..5214815 --- /dev/null +++ b/Python/p003.py @@ -0,0 +1,36 @@ +#!/usr/bin/python3 + +import math + +from timeit import default_timer +from projecteuler import is_prime + +def max_prime_factor(num): + if is_prime(num): + return num + + if num % 2 == 0: + return max_prime_factor(num / 2) + + else: + limit = math.floor(math.sqrt(num)) + 1 + + for i in range(3, limit, 2): + if num % i == 0: + if is_prime(i): + return max_prime_factor(num / i) + +def main(): + start = default_timer() + + res = max_prime_factor(600851475143) + + end = default_timer() + + print('Project Euler, Problem 3') + print('Answer: {}'.format(int(res))) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p004.py b/Python/p004.py new file mode 100644 index 0000000..3be92af --- /dev/null +++ b/Python/p004.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 + +from timeit import default_timer + +def is_palindrome(num, base): + reverse = 0 + + tmp = num + + while tmp > 0: + reverse = reverse * base + reverse = reverse + tmp % base + tmp = int(tmp / base) + + if num == reverse: + return 1 + + return 0 + +def main(): + start = default_timer() + + max_ = 0 + + for i in range(999, 99, -1): + for j in range(i, 99, -1): + num = i * j + if num > max_ and is_palindrome(num, 10): + max_ = num + + end = default_timer() + + print('Project Euler, Problem 4') + print('Answer: {}'.format(max_)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p005.py b/Python/p005.py new file mode 100644 index 0000000..a68e89d --- /dev/null +++ b/Python/p005.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +from timeit import default_timer +from projecteuler import lcmm + +def main(): + start = default_timer() + + values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) + + res = lcmm(values, 20) + + end = default_timer() + + print('Project Euler, Problem 5') + print('Answer: {}'.format(int(res))) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p006.py b/Python/p006.py new file mode 100644 index 0000000..4c8e17b --- /dev/null +++ b/Python/p006.py @@ -0,0 +1,24 @@ +#!/usr/bin/python3 + +from timeit import default_timer +def main(): + start = default_timer() + + sum_squares = 0 + square_sum = 0 + + for i in range(1, 101): + sum_squares = sum_squares + i * i + square_sum = square_sum + i + + square_sum = square_sum * square_sum + + end = default_timer() + + print('Project Euler, Problem 6') + print('Answer: {}'.format(square_sum - sum_squares)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p007.py b/Python/p007.py new file mode 100644 index 0000000..f5b4020 --- /dev/null +++ b/Python/p007.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 + +from timeit import default_timer +from projecteuler import is_prime + +def main(): + start = default_timer() + + count = 1 + n = 1 + + while count != 10001: + n = n + 2 + + if is_prime(n): + count = count + 1 + + end = default_timer() + + print('Project Euler, Problem 7') + print('Answer: {}'.format(n)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p008.py b/Python/p008.py new file mode 100644 index 0000000..b046f80 --- /dev/null +++ b/Python/p008.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 + +from timeit import default_timer + +def main(): + start = default_timer() + + number = '73167176531330624919225119674426574742355349194934' +\ + '96983520312774506326239578318016984801869478851843' +\ + '85861560789112949495459501737958331952853208805511' +\ + '12540698747158523863050715693290963295227443043557' +\ + '66896648950445244523161731856403098711121722383113' +\ + '62229893423380308135336276614282806444486645238749' +\ + '30358907296290491560440772390713810515859307960866' +\ + '70172427121883998797908792274921901699720888093776' +\ + '65727333001053367881220235421809751254540594752243' +\ + '52584907711670556013604839586446706324415722155397' +\ + '53697817977846174064955149290862569321978468622482' +\ + '83972241375657056057490261407972968652414535100474' +\ + '16427171479924442928230863465674813919123162824586' +\ + '17866458359124566529476545682848912883142607690042' +\ + '24219022671055626321111109370544217506941658960408' +\ + '07198403850962455444362981230987879927244284909188' +\ + '84580156166097919133875499200524063689912560717606' +\ + '05886116467109405077541002256983155200055935729725' +\ + '71636269561882670428252483600823257530420752963450' + + number = list(map(int, number)) + + max_ = 0 + + for i in range(1000-13): + curr = number[i:i+13] + prod = 1 + + for j in range(len(curr)): + prod = prod * curr[j] + + if prod > max_: + max_ = prod + + end = default_timer() + + print('Project Euler, Problem 8') + print('Answer: {}'.format(max_)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p009.py b/Python/p009.py new file mode 100644 index 0000000..a7380d3 --- /dev/null +++ b/Python/p009.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 + +from timeit import default_timer + +def main(): + start = default_timer() + + found = 0 + + m = 2 + + while not found: + for n in range(1, m): + a = m * m - n * n + b = 2 * m * n + c = m * m + n * n + + if a + b + c == 1000: + found = 1 + break + + m = m + 1 + + end = default_timer() + + print('Project Euler, Problem 9') + print('Answer: {}'.format(a * b * c)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p010.py b/Python/p010.py new file mode 100644 index 0000000..b0e214f --- /dev/null +++ b/Python/p010.py @@ -0,0 +1,24 @@ +#!/usr/bin/python3 + +from timeit import default_timer +from projecteuler import sieve + +def main(): + start = default_timer() + + primes = sieve(2000000) + sum_ = 0 + + for i in range(2000000): + if primes[i]: + sum_ = sum_ + i + + end = default_timer() + + print('Project Euler, Problem 10') + print('Answer: {}'.format(sum_)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/projecteuler.py b/Python/projecteuler.py new file mode 100644 index 0000000..74ad95d --- /dev/null +++ b/Python/projecteuler.py @@ -0,0 +1,60 @@ +#!/usr/bin/python3 + +import math +from numpy import ndarray + +def is_prime(num): + if num < 4: + return num == 2 or num == 3 + + if num % 2 == 0 or num % 3 == 0: + return 0 + + limit = math.floor(math.sqrt(num)) + 1 + + for i in range(5, limit, 6): + if num % i == 0 or num % (i + 2) == 0: + return 0 + + return 1 + +def gcd(a, b): + while b != 0: + tmp = b + b = a % b + a = tmp + + return a + +def lcm(a, b): + return a * b / gcd(a, b) + +def lcmm(values, n): + if n == 2: + return lcm(values[0], values[1]) + + value = values[0] + + for i in range(1, n): + return lcm(value, lcmm(values[i:], n-1)) + +def sieve(n): + primes = ndarray((n,), int) + + primes[0] = 0 + primes[1] = 0 + primes[2] = 1 + primes[3] = 1 + + for i in range(4, n, 2): + primes[i] = 0 + primes[i+1] = 1 + + limit = math.floor(math.sqrt(n)) + + for i in range(3, limit, 2): + if primes[i]: + for j in range(i * i, n, 2 * i): + primes[j] = 0 + + return primes