Shanks's square forms factorization explained

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

and

y

such that

x2-y2=N

, where

N

is the integer to be factored. An improvement (noticed by Kraitchik) is to look for integers

x

and

y

such that

x2\equivy2\pmod{N}

. Finding a suitable pair

(x,y)

does not guarantee a factorization of

N

, but it implies that

N

is a factor of

x2-y2=(x-y)(x+y)

, and there is a good chance that the prime divisors of

N

are distributed between these two factors, so that calculation of the greatest common divisor of

N

and

x-y

will give a non-trivial factor of

N

.

A practical algorithm for finding pairs

(x,y)

which satisfy

x2\equivy2\pmod{N}

was developed by Shanks, who named it Square Forms Factorization or SQUFOF. The algorithm can be expressed in terms of continued fractions or in terms of quadratic forms. Although there are now much more efficient factorization methods available, SQUFOF has the advantage that it is small enough to be implemented on a programmable calculator. Shanks programmed it on an HP-65, made in 1974, which has storage for only nine digit numbers and allows only 100 steps/keystrokes of programming. There are versions of the algorithm that use little memory and versions that store a list of values that run more quickly.

In 1858, the Czech mathematician Václav Šimerka used a method similar to SQUFOF to factor

(1017-1)/9

=

11111111111111111

=

20717235363222357

.[1]

Algorithm

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

, the integer to be factored, which must be neither a prime number nor a perfect square, and a small positive integer,

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
i=\left\lfloorP0+Pi-1
Qi-1

\right\rfloor,Pi=biQi-1-Pi-1,Qi=Qi-2+bi(Pi-1-Pi)

until

Qi

is a perfect square at some odd value of

i

.

Start the second phase (reverse cycle).

Initialize

b
0=\left\lfloorP0-Pi
\sqrt{Qi
}\right\rfloor,

Q-1=\sqrt{Qi}

, and

P0=b0\sqrt{Qi}+Pi

, where

P0,Pi

, and

Qi

are from the previous phase. The

b0

used in the calculation of

P0

is the recently calculated value of

b0

.

Set

i=0

and
Q
0=
2
kN-P
0
Q-1
, where

P0

is the recently calculated value of

P0

.

Repeat

i=i+1,b
i=\left\lfloorP0+Pi-1
Qi-1

\right\rfloor,Pi=biQi-1-Pi-1,Qi=Qi-2+bi(Pi-1-Pi)

until

Pi=Pi-1.

Then if

f=\gcd(N,Pi)

is not equal to

1

and not equal to

N

, then

f

is a non-trivial factor of

N

. Otherwise try another value of

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]

Example

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

is a perfect square, so the first phase ends.

For the second phase, set

Q-1=\sqrt{25}=5

. Then:
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

, so the second phase ends. Now calculate

gcd(11111,82)=41

, which is a factor of

11111

.

Thus,

N=11111=41271

.

Example implementation

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.

  1. include
  2. define nelems(x) (sizeof(x) / sizeof((x)[0]))

const int multiplier[] = ;

uint64_t SQUFOF(uint64_t N)

References

. 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 .

External links

Notes and References

  1. Lemmermeyer . F. . Václav Šimerka: quadratic forms and factorization . LMS Journal of Computation and Mathematics . 2013 . 16 . 118–129 . 10.1112/S1461157013000065 . free.
  2. Daniel Shanks' Square Forms Factorization. 2004 . 10.1.1.107.9984.