Found the biggest factor: 6857
This commit is contained in:
55
3/main.py
Normal file
55
3/main.py
Normal file
@@ -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()
|
||||||
Reference in New Issue
Block a user