Shanks's square forms factorization


Shanks's 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 and such that, where is the integer to be factored. An improvement is to look for integers and such that. Finding a suitable pair does not guarantee a factorization of, but it implies that is a factor of, and there is a good chance that the prime divisors of are distributed between these two factors, so that calculation of the greatest common divisor of and will give a non-trivial factor of.
A practical algorithm for finding pairs which satisfy 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.
In 1858, the Czech mathematician Václav Šimerka used a method similar to SQUFOF to factor .

Algorithm

Input:, the integer to be factored, which must be neither a prime number nor a perfect square, and a small multiplier.
Output: a non-trivial factor of.
The algorithm:
Initialize
Repeat
until is a perfect square at some even.
Initialize
Repeat
until
Then if is not equal to and not equal to, then is a non-trivial factor of. Otherwise try another value of.
Shanks's method has time complexity.
Stephen S. McMath wrote
a more detailed discussion of the mathematics of Shanks's method,
together with a proof of its correctness.

Example

Let
Here is a perfect square.
Here.
, which is a factor of.
Thus,

Example implementations

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 / sizeof)
const int multiplier = ;
uint64_t SQUFOF