From 04f4e37a282d221bce12eb5e15426476a2e3da4b Mon Sep 17 00:00:00 2001 From: "adrian.spycher" <adrian.spycher@etu.hesge.ch> Date: Mon, 9 Jan 2023 20:38:30 +0100 Subject: [PATCH] optimize fermat_factorization --- algo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/algo.py b/algo.py index 4098387..ae2f722 100644 --- a/algo.py +++ b/algo.py @@ -78,7 +78,7 @@ def exponentiation_rapide(a, exp, n): 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, from : https://stackoverflow.com/questions/2489435/check-if-a-number-is-a-perfect-square @@ -121,7 +121,7 @@ def fermat_factorization(n): a = math.ceil(math.sqrt(n)) # a = 26262277040 (for the key given) 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 b2 = a**2 - n -- GitLab