From 4599d7be125ac271b3da102c0208275c6540961b Mon Sep 17 00:00:00 2001 From: t3xhno Date: Tue, 6 Feb 2024 15:00:05 +0100 Subject: [PATCH] Problem3 --- lib/num_fns.py | 27 ++++++++++++++++++++++----- src/problem3.py | 14 ++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/problem3.py diff --git a/lib/num_fns.py b/lib/num_fns.py index d7cedf3..28b60ee 100644 --- a/lib/num_fns.py +++ b/lib/num_fns.py @@ -1,9 +1,26 @@ +import math + def isDivBy(num, denom): return num % denom == 0 def fib(i): - if i == 1: - return 1 - elif i == 2: - return 2 - return fib(i - 1) + fib(i - 2) \ No newline at end of file + if i == 1: return 1 + elif i == 2: return 2 + return fib(i - 1) + fib(i - 2) + +def sieve(num): + prime = [True for i in range(num + 1)] + p = 2 + while p * p <= num: + if prime[p] == True: + for i in range(p * p, num + 1, p): + prime[i] = False + p += 1 + + primes = [] + + for p in range(2, num + 1): + if prime[p]: + primes.append(p) + + return primes diff --git a/src/problem3.py b/src/problem3.py new file mode 100644 index 0000000..6ae1de6 --- /dev/null +++ b/src/problem3.py @@ -0,0 +1,14 @@ +import math +import sys +sys.path.append("lib") +from num_fns import sieve, isDivBy + +target = 600851475143 + +primes = sieve(math.floor(math.sqrt(target))) +primes.reverse() + +for i in primes: + if isDivBy(target, i): + print(i) + break