diff --git a/3/main.py b/3/main.py new file mode 100644 index 0000000..f0e65aa --- /dev/null +++ b/3/main.py @@ -0,0 +1,55 @@ +import os +import numpy as np + + +# Find the largest prime factor of 600851475143 + + +def isDivisible(n, ms): + res = False + for m in ms: + if n % m == 0: + res = True + break + return res + + +def getNextPrime(ps): + if len(ps) == 0: + return [2] + else: + p = ps[-1] + 1 + while isDivisible(p, [q for q in ps if q < np.sqrt(p)]): + p = p + 1 + ps.append(p) + return ps + +def findFactors(n): + ps = [2] + res = [] + rest = n + stop = np.sqrt(n) + + while rest != 1: + if rest % ps[-1] == 0: + res.append(ps[-1]) + rest = rest / ps[-1] + else: + ps = getNextPrime(ps) + if stop < ps[-1]: + break + return res + + + +def main(): + print("Hello, this is Patrick") + + number = 600851475143 + fs = findFactors(number) + + print(max(fs)) + + +if __name__ == "__main__": + main() \ No newline at end of file