-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSQUFOF.py
50 lines (47 loc) · 1.2 KB
/
SQUFOF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Code borrowed and adapted from the wikipedia: https://en.wikipedia.org/wiki/Shanks%27s_square_forms_factorization
# It may contain bugs
def SQUFOF(N):
s = int( isqrt(N)+0.5)
if (s**2 == N):
return s
for k in range(0,len(multiplier)):
D = multiplier[k]*N
Po = Pprev = P = isqrt(D)
Qprev = 1
Q = D - Po**2
L = int(2 * isqrt(2*s))
B = 3 * L
for i in range(2,B):
b = int((Po + P)//Q)
P = b*Q - P
q = Q
Q = Qprev + b*(Pprev - P)
r = int(isqrt(Q)+0.5)
if (not(i & 1) and ((r**2) == Q)):
break
Qprev = q
Pprev = P
if (i >= B):
continue
b = ((Po - P)//r)
Pprev = P = b*r + P
Qprev = r
Q = (D - Pprev**2)//Qprev
i = 0
c = True
while(c):
b = int((Po + P)//Q)
Pprev = P
P = b*Q - P
q = Q;
Q = Qprev + b*(Pprev - P)
Qprev = q
i+=1
c = (P != Pprev)
r = gcd(N, Qprev)
if (1 < r < N):
return r
return -1
N=int(sys.argv[1])
print(SQUFOF(N))
@daedalus