Fermat's factorization method, named after Pierre de Fermat, is based on the representation of an odd integer as the difference of two squares:
N=a2-b2.
(a+b)(a-b)
Each odd number has such a representation. Indeed, if
N=cd
N=\left(
c+d | |
2 |
\right)2-\left(
c-d | |
2 |
\right)2
Since N is odd, then c and d are also odd, so those halves are integers. (A multiple of four is also a difference of squares: let c and d be even.)
In its simplest form, Fermat's method might be even slower than trial division (worst case). Nonetheless, the combination of trial division and Fermat's is more effective than either by itself.
One tries various values of a, hoping that
a2-N=b2
FermatFactor(N): // N should be odd a ← b2 ← a*a - N repeat until b2 is a square: a ← a + 1 b2 ← a*a - N // equivalently: // b2 ← b2 + 2*a + 1 // a ← a + 1 return a - // or a +
For example, to factor
N=5959
b2=782-5959=125
Try: | 1 | 2 | 3 | |
---|---|---|---|---|
a | 78 | 79 | 80 | |
b2 | 125 | 282 | 441 | |
b | 11.18 | 16.79 | 21 |
The third try produces the perfect square of 441. Thus,
a=80
b=21
a-b=59
a+b=101
Suppose N has more than two prime factors. That procedure first finds the factorization with the least values of a and b. That is,
a+b
a-b=N/(a+b)
N=1 ⋅ N
For
N=cd
a=(c+d)/2
(c+d)/2-\sqrtN=(\sqrtd-\sqrtc)2/2=(\sqrtN-c)2/2c
If N is prime (so that
c=1
O(N)
{\left(4N\right)}1/4
\sqrtN
Consider trying to factor the prime number, but also compute b and throughout. Going up from
\sqrt{N}
Try | |||||
---|---|---|---|---|---|
a | 48,433 | 48,434 | 48,435 | 48,436 | |
b2 | 76,572 | 173,439 | 270,308 | 367,179 | |
b | 276.7 | 416.5 | 519.9 | 605.9 | |
a − b | 48,156.3 | 48,017.5 | 47,915.1 | 47,830.1 |
In practice, one wouldn't bother with that last row until b is an integer. But observe that if N had a subroot factor above
a-b=47830.1
Trial division would normally try up to 48,432; but after only four Fermat steps, we need only divide up to 47830, to find a factor or prove primality.
This all suggests a combined factoring method. Choose some bound
amax>\sqrt{N}
\sqrt{N}
amax
amax-
2 | |
\sqrt{a | |
max |
-N}
amax=48436
amax=55000
In this regard, Fermat's method gives diminishing returns. One would surely stop before this point:
a | 60,001 | 60,002 | |
---|---|---|---|
b2 | 1,254,441,084 | 1,254,561,087 | |
b | 35,418.1 | 35,419.8 | |
a − b | 24,582.9 | 24,582.2 |
When considering the table for
N=2345678917
b2
a | 48,433 | 48,434 | 48,435 | 48,436 | |
---|---|---|---|---|---|
b2 | 76,572 | 173,439 | 270,308 | 367,179 | |
b | 276.7 | 416.5 | 519.9 | 605.9 |
It is not necessary to compute all the square-roots of
a2-N
a2-N
a2
b2
This can be performed with any modulus. Using the same
N=2345678917
modulo 16: | Squares are | 0, 1, 4, or 9 | |
N mod 16 is | 5 | ||
so a2 | 9 | ||
and must be | 3 or 5 or 11 or 13 modulo 16 | ||
modulo 9: | Squares are | 0, 1, 4, or 7 | |
N mod 9 is | 7 | ||
so a2 | 7 | ||
and must be | 4 or 5 modulo 9 |
Given a sequence of a-values (start, end, and step) and a modulus, one can proceed thus:
, astart, aend, astep, modulus) a ← astart do modulus times: b2 ← a*a - N if b2 is a square, modulo modulus:, a, aend, astep * modulus, NextModulus) endif a ← a + astep enddo
But the recursion is stopped when few a-values remain; that is, when / is small. Also, because as step-size is constant, one can compute successive b2's with additions.
Fermat's method works best when there is a factor near the square-root of N.
If the approximate ratio of two factors (
d/c
v/u
Nuv=cv ⋅ du
cv
du
\gcd(N,cv)=c
\gcd(N,du)=d
Generally, if the ratio is not known, various
u/v
O(N1/3)
The fundamental ideas of Fermat's factorization method are the basis of the quadratic sieve and general number field sieve, the best-known algorithms for factoring large semiprimes, which are the "worst-case". The primary improvement that quadratic sieve makes over Fermat's factorization method is that instead of simply finding a square in the sequence of
a2-n