We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2c439f5 commit c1a2954Copy full SHA for c1a2954
fermat.py
@@ -1,20 +1,16 @@
1
import sys
2
-from gmpy2 import isqrt
+from gmpy2 import isqrt, isqrt_rem, is_square
3
4
def fermat(n):
5
- """Fermat attack"""
6
- a = isqrt(n)
7
- a = b
8
- b2 = (a ** 2) - n
9
- count = 0
10
- while (b ** 2) != b2:
11
- a = a + 1
12
13
- b = isqrt(b2)
14
- count += 1
15
- p = a + b
16
- q = a - b
17
- assert n == p * q
18
- return p, q
+ a, rem = isqrt_rem(n)
+ b2 = -rem
+ c0 = (a << 1) + 1
+ c = c0
+ while not is_square(b2):
+ b2 += c
+ c += 2
+ a = (c - 1) >> 1
+ b = isqrt(b2)
+ return a - b, a + b
19
20
print(fermat(int(sys.argv[1])))
0 commit comments