|
@@ -0,0 +1,21 @@
|
|
|
+### Project Euler problem 3 (estelendur)
|
|
|
+# What is the largest prime factor of 600851475143?
|
|
|
+
|
|
|
+import math
|
|
|
+
|
|
|
+def not_square(b2):
|
|
|
+ root = math.sqrt(b2)
|
|
|
+ return (math.ceil(root) != math.floor(root))
|
|
|
+
|
|
|
+def fermat_factor(N):
|
|
|
+ if(N%2==0): # N should be odd
|
|
|
+ fermat_factor(N/2)
|
|
|
+ else:
|
|
|
+ a = math.ceil(math.sqrt(N))
|
|
|
+ b2 = a**2 - N
|
|
|
+ while not_square(b2):
|
|
|
+ a += 1
|
|
|
+ b2 = a**2 - N
|
|
|
+ return (a-math.sqrt(b2))
|
|
|
+
|
|
|
+print fermat_factor(600851475143)
|