Shanks' square forms factorization is a method for integer factorization devised by Daniel Shanks as an improvement on Fermat's factorization method.
The success of Fermat's method depends on finding integers
x
y
x2-y2=N
N
x
y
x2\equivy2\pmod{N}
(x,y)
N
N
x2-y2=(x-y)(x+y)
N
N
x-y
N
A practical algorithm for finding pairs
(x,y)
x2\equivy2\pmod{N}
In 1858, the Czech mathematician Václav Šimerka used a method similar to SQUFOF to factor
(1017-1)/9
=
11111111111111111
=
2071723 ⋅ 5363222357
Note This version of the algorithm works on some examples but often gets stuck in a loop.
This version does not use a list.
Input:
N
k
Output: a non-trivial factor of
N
The algorithm:
Initialize
i=0,P0=\lfloor\sqrt{kN}\rfloor,Q-1=1,Q0=kN-P
2. | |
0 |
Repeat
i=i+1,b | ||||
|
\right\rfloor,Pi=biQi-1-Pi-1,Qi=Qi-2+bi(Pi-1-Pi)
until
Qi
i
Start the second phase (reverse cycle).
Initialize
b | ||||
|
Q-1=\sqrt{Qi}
P0=b0\sqrt{Qi}+Pi
P0,Pi
Qi
b0
P0
b0
Set
i=0
Q | ||||||||||
|
P0
P0
Repeat
i=i+1,b | ||||
|
\right\rfloor,Pi=biQi-1-Pi-1,Qi=Qi-2+bi(Pi-1-Pi)
until
Pi=Pi-1.
Then if
f=\gcd(N,Pi)
1
N
f
N
k
Shanks' method has time complexity
O(\sqrt[4]{N})
Stephen S. McMath wrotea more detailed discussion of the mathematics of Shanks' method,together with a proof of its correctness.[2]
Let
N=11111
Q-1=1
Cycle forward | - ! i | bi Pi | Qi | - | 0 | 105 | 86 | ||||
---|---|---|---|---|---|---|---|---|---|---|---|
1 | 2 | 67 | 77 | ||||||||
2 | 2 | 87 | 46 | ||||||||
3 | 4 | 97 | 37 | ||||||||
4 | 5 | 88 | 91 | ||||||||
5 | 2 | 94 | 25 |
Here
Q5=25
For the second phase, set
Q-1=\sqrt{25}=5
Reverse cycle | - ! i | bi Pi | Qi | - | 0 | 2 | 104 | 59 | |||
---|---|---|---|---|---|---|---|---|---|---|---|
1 | 3 | 73 | 98 | ||||||||
2 | 1 | 25 | 107 | ||||||||
3 | 1 | 82 | 41 | ||||||||
4 | 4 | 82 |
Here
P3=P4=82
gcd(11111,82)=41
11111
Thus,
N=11111=41 ⋅ 271
Below is an example of C function for performing SQUFOF factorization on unsigned integer not larger than 64 bits, without overflow of the transient operations.
const int multiplier[] = ;
uint64_t SQUFOF(uint64_t N)
. D. M. Bressoud. David Bressoud. Factorisation and Primality Testing. Springer-Verlag. 1989. 0-387-97040-1. registration.
. Hans Riesel. Prime Numbers and Computer Methods for Factorization. Birkhauser. 2nd. 1994. 0-8176-3743-5 .