FACTOID # 154: Women make up more than 10% of the prison population in only six countries: Thailand, , Qatar, Paraguay, Costa Rica, and Singapore.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Fermat's factorization method

With Fermat's factoring method, one tries to represent an odd integer as the difference of two squares: N = a2b2. That difference is algebraically factorable as (a + b)(ab); if neither factor equals one, it is a proper factorization of N.


Furthermore, each odd number has such a representation. If N = cd, then N = [(c + d) / 2]2 − [(cd) / 2]2. Since c and d are both odd, 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 is even slower than trial division (on average). Nonetheless, the combination of trial division and Fermat's is more effective than either.

Contents


The basic method

One tries various values of a, hoping that a2N = b2 is a square.

FermatFactor(N): // N should be odd
A ← ceil(sqrt(N))
Bsq ← A*A - N
while Bsq isn't a square:
A ← A + 1
Bsq ← A*A - N // equivalently: Bsq ← Bsq + 2*A - 1
endwhile
return A - sqrt(Bsq) // or A + sqrt(Bsq)

For example, to factor N = 5959, one computes

A: 78 79 80
Bsq: 125 282 441

The third try produces a square. A = 80, B = 21, and the factors are AB = 59, and A + B = 101.


Suppose N has more than one factorization. That procedure first finds the factorization with the least values of a and b. That is, a + b is the smallest factor ≥ the square-root of N. And so ab = N / (a + b) is the largest factor ≤ root-N. If the procedure finds N = 1 * N, that shows that N is prime.


For N = cd, let c be the largest subroot factor. a = (c + d) / 2, so the number of steps is approximately .


If N is prime (so that c = 1), one needs O(N) steps! This is a bad way to prove primality. But if N has a factor close to its square-root, the method works quickly.


Fermat's and trial division

Let's factor the prime number N=2345678917, but also compute B and A-B throughout.

A: 48433 48434 48435 48436
Bsq: 76572 173439 270308 367179
B: 276.7 416.5 519.9 605.9
A-B: 48156.3 48017.5 47915.1 47830.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 AB = 47830.1, Fermat's method would have found it already.


Trial division would normally try up to 48432; but after only four Fermat steps, we need only divide up to 47830, to find a factor or prove primality.


In this regard, Fermat's gives diminishing returns. One would probably stop long before this point:

A: 60001 60001
Bsq: 1254441084 1254561087
B: 35418.1 35419.8
A-B: 24582.9 24582.2

This all suggests a combined factoring method. Choose some bound c; use trial division to find factors below c, and Fermat's for factors above c. That is, do Fermat until ab < c, or a > (c + N / c) / 2. The best choice of c depends on N, and on the computing environment.


It also depends on the algorithm. There are ways to speed-up the basic method.


Sieve Improvement

One needn't compute all those square-roots of a2N. Look again at this tableau for N = 2345678917.

A: 48433 48434 48435 48436
Bsq: 76572 173439 270308 367179
B: 276.7 416.5 519.9 605.9

One can tell at a glance that the first and third values of Bsq aren't squares. Squares end with 0, 1, 4, 5, 6, or 9. Not only that: the 11th and 13th values aren't squares, either. If a is increased by 10, Bsq will end with the same digit. One finds that a must end in 1 4 6 or 9, to make a square.


This can be generalized to any modulus. For that same N,

modulo 16: Bsq must be 0 1 4 or 9
so A must be 3 5 11 or 13
modulo 9: Bsq must be 0 1 4 or 7
so A must be 4 or 5
etc.

One generally chooses a power of a different prime for each modulus.


Given a sequence of a-values (start, end, and step) and a modulus, one can proceed thus:

FermatSieve(N, Astart, Aend, Astep, Modulus)
A ← Astart
do Modulus times:
Bsq ← A*A - N
if Bsq is a square, modulo Modulus:
FermatSieve(N, A, Aend, Astep * Modulus, NextModulus)
endif
A ← A + Astep
enddo

But one stops the recursion, when few a-values remain; that is, when (Aend-Astart)/Astep is small. Also, because a's step-size is constant, one can compute successive Bsq's with additions.


Multiplier improvement

Fermat's method works best when there's a factor near the square-root of N. Perhaps one can arrange for that to happen.


If one knew the approximate ratio of two factors (d / c), then one could pick a rational number u / v near that value. Nuv = cv * du, and the factors are roughly equal: Fermat's, applied to Nuv, will find them quickly. Then gcd(N,cv) = c and gcd(N,du) = d. (Unless c divides u or d divides v.)


Generally, one doesn't know the ratio, but one can try various u / v values, and try to factor each resulting Nuv. R. Lehman devised a systematic way to do this, so that Fermat's plus trial-division can factor N in O(N1 / 3) time. See R. Lehman, "Factoring Large Integers", Mathematics of Computation, 28:637-646, 1974.


Other improvements

See also J. McKee, "Speeding Fermat's factoring method", Mathematics of Computation, 68:1729-1737, 1999.



 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.