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


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


Adding and subtracting,


so solving for x and y gives




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


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




Continue with


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),

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.

See also

Prime Factorization Algorithms, Smooth Number

Explore with Wolfram|Alpha


Lehmer, D. H. and Powers, R. E. "On Factoring Large Numbers." Bull. Amer. Math. Soc. 37, 770-776, 1931.McKee, J. "Speeding Fermat's Factoring Method." Math. Comput. 68, 1729-1738, 1999.Morrison, M. A. and Brillhart, J. "A Method of Factoring and the Factorization of F_7." Math. Comput. 29, 183-205, 1975.Pomerance, C. "A Tale of Two Sieves." Not. Amer. Math. Soc. 43, 1473-1485, 1996.

Referenced on Wolfram|Alpha

Fermat's Factorization Method

Cite this as:

Weisstein, Eric W. "Fermat's Factorization Method." From MathWorld--A Wolfram Web Resource.

Subject classifications