Fermat's Factorization Method

Given a number n, Fermat's factorization methods look for integers x and y such that n=x^2-y^2. Then

 n=(x-y)(x+y)
(1)

and n is factored. A modified form of this observation leads to Dixon's factorization method and the quadratic sieve.

Every positive odd integer can be represented in the form n=x^2-y^2 by writing n=ab (with a>b) and noting that this gives

a=x+y
(2)
b=x-y.
(3)

Adding and subtracting,

a+b=2x
(4)
a-b=2y,
(5)

so solving for x and y gives

x=1/2(a+b)
(6)
y=1/2(a-b).
(7)

Therefore,

 x^2-y^2=1/4[(a+b)^2-(a-b)^2]=ab.
(8)

As the first trial for x, try x_1=[sqrt(n)], where [x] is the ceiling function. Then check if

 Deltax_1=x_1^2-n
(9)

is a square number. There are only 22 combinations of the last two digits which a square number can assume, so most combinations can be eliminated. If Deltax_1 is not a square number, then try

 x_2=x_1+1,
(10)

so

Deltax_2=x_2^2-n
(11)
=(x_1+1)^2-n
(12)
=x_1^2+2x_1+1-n
(13)
=Deltax_1+2x_1+1.
(14)

Continue with

Deltax_3=x_3^2-n
(15)
=(x_2+1)^2-n
(16)
=x_2^2+2x_2+1-n
(17)
=Deltax_2+2x_2+1
(18)
=Deltax_2+2x_1+3,
(19)

so subsequent differences are obtained simply by adding two.

Maurice Kraitchik sped up the algorithm by looking for x and y satisfying

 x^2=y^2 (mod n),
(20)

i.e., n|(x^2-y^2). This congruence has uninteresting solutions x=+/-y (mod n) and interesting solutions x≢+/-y (mod n). It turns out that if n is odd and divisible by at least two different primes, then at least half of the solutions to x^2=y^2 (mod n) with xy relatively prime to n are interesting. For such solutions, (n,x-y) is neither n nor 1 and is therefore a nontrivial factor of n (Pomerance 1996). This algorithm can be used to prove primality, but is not practical. In 1931, Lehmer and Powers discovered how to search for such pairs using continued fractions. This method was improved by Morrison and Brillhart (1975) into the continued fraction factorization algorithm, which was the fastest algorithm in use before the quadratic sieve factorization method was developed.

Wolfram Web Resources

Mathematica »

The #1 tool for creating Demonstrations and anything technical.

Wolfram|Alpha »

Explore anything with the first computational knowledge engine.

Wolfram Demonstrations Project »

Explore thousands of free applications across science, mathematics, engineering, technology, business, art, finance, social sciences, and more.

Computerbasedmath.org »

Join the initiative for modernizing math education.

Online Integral Calculator »

Solve integrals with Wolfram|Alpha.

Step-by-step Solutions »

Walk through homework problems step-by-step from beginning to end. Hints help you try the next step on your own.

Wolfram Problem Generator »

Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.

Wolfram Education Portal »

Collection of teaching and learning tools built by Wolfram education experts: dynamic textbook, lesson plans, widgets, interactive Demonstrations, and more.

Wolfram Language »

Knowledge-based programming for everyone.