Skip to content
Snippets Groups Projects
Commit 04f4e37a authored by adrian.spycher's avatar adrian.spycher
Browse files

optimize fermat_factorization

parent 38cdacc5
Branches
No related tags found
No related merge requests found
...@@ -78,7 +78,7 @@ def exponentiation_rapide(a, exp, n): ...@@ -78,7 +78,7 @@ def exponentiation_rapide(a, exp, n):
return int(r) return int(r)
def is_square(a): def is_perfect_square(a):
"""Check if a number is a perfect square, based on the "Babylonian algorithm" (Héron's method) for square root, """Check if a number is a perfect square, based on the "Babylonian algorithm" (Héron's method) for square root,
from : https://stackoverflow.com/questions/2489435/check-if-a-number-is-a-perfect-square from : https://stackoverflow.com/questions/2489435/check-if-a-number-is-a-perfect-square
...@@ -121,7 +121,7 @@ def fermat_factorization(n): ...@@ -121,7 +121,7 @@ def fermat_factorization(n):
a = math.ceil(math.sqrt(n)) # a = 26262277040 (for the key given) a = math.ceil(math.sqrt(n)) # a = 26262277040 (for the key given)
b2 = a**2 - n b2 = a**2 - n
while (not is_square(b2)): while ((a + int(math.sqrt(b2))) * (a - int(math.sqrt(b2))) != n): # (not is_perfect_square(b2)):
a += 1 a += 1
b2 = a**2 - n b2 = a**2 - n
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment