ArticleslgStudy

computer science

Fermat's factorization method

Fermat's factorization method is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Fermat's factorization method rather than just read about it. In short: 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. That difference is algebraically factorable as (a+b)(a−b); if neither factor equals one, it is a proper factorization of N.

Key takeaways

  • Fermat's factorization method belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Fermat's factorization method to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Fermat's factorization method from memory before moving on to harder problems.

Reference excerpt

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. That difference is algebraically factorable as (a+b)(a−b); if neither factor equals one, it is a proper factorization of N. Every odd number can be represented as such. If N=cd is a factorization of N, then: N=(c+d2)2−(c−d2)2. Since N is odd, both c and d are also odd, and hence the two terms in parentheses are integers. In its simplest form, Fermat's method can be even slower than trial division (in the worst case). Nonetheless, combining Fermat's method and trial division can be more efficient than either method alone.

Basic method One tries various values of a, hoping that a2−N=b2, a square.

FermatFactor(N): // N should be odd a ← ceiling(sqrt(N)) 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 - sqrt(b2) // or a + sqrt(b2)

For example, to factor N=5959, the first try for a is the square root of 5959 rounded up to the next integer, which is 78. Then b2=782−5959=125. Since 125 is not a square, a second try is made by increasing the value of a by 1. The second attempt also fails, because 282 is again not a square.

The third try produces the perfect square of 441. Thus, a=80, b=21, and the factors of 5959 are a−b=59 and 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 is the smallest factor ≥ the square-root of N, and so a−b=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 (c+d)/2−N=(d−c)2/2=(N−c)2/2c. 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. More precisely, if c differs less than (4N)1/4 from N, the method requires only one step; this is independent of the size of N.

Fermat's and trial division Consider trying to factor the prime number N = 2,345,678,917, but also compute b and a − b throughout. Going up from N rounded up to the next integer, which is 48,433, we can tabulate:

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, Fermat's method would have found it already. 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>N; use Fermat's method for factors between N and amax. This gives a bound for trial division which is amax−amax2−N. In the above example, with amax=48436 the bound for trial division is 47830. A reasonable choice could be amax=55000 giving a bound of 28937. In this regard, Fermat's method gives diminishing returns. One would surely stop before this point:

Sieve improvement When considering the table for N=2345678917, one can quickly tell that none of the values of b2 are squares:

It is not necessary to compute all the square-roots of a2−N, nor even examine all the values for a. Squares are always congruent to 0, 1, 4, 5, 9, 16 modulo 20, because these are the quadratic residues of 20. The values repeat with each increase of a by 10. In this example, N is 17 mod 20, so subtracting 17 mod 20 (or adding 3), a2−N produces 3, 4, 7, 8, 12, and 19 modulo 20 for these values. It is apparent that only the 4 from this list can be a square. Thus, a2 must be 1 mod 20, which means that a is 1, 9, 11 or 19 mod 20; it will produce a b2 which ends in 4 mod 20 and, if square, b will end in 2 or 8 mod 10. This can be performed with any modulus. Using the same N=2345678917,

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: b2 ← a*a - N if b2 is a square, modulo modulus: FermatSieve(N, a, aend, astep * modulus, NextModulus) endif a ← a + astep enddo

But the recursion is stopped 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 b2's with additions.

Multiplier improvement 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) is known, then a rational number v/u can be picked near that value. Nuv=cv⋅du, and Fermat's method, applied to Nuv, will find the factors cv and du quickly. Then gcd⁡(N,cv)=c and gcd⁡(N,du)=d. (Unless c divides u or d divides v.) Generally, if the ratio is not known, various u/v values can be tried, 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.

Other improvements 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, it finds a subset of elements of this sequence whose product is a square, and it does this in a highly efficient manner. The end result is the same: a difference of squares mod n that, if nontrivial, can be used to factor n.

See also Completing the square Factorization of polynomials Factor theorem FOIL rule Monoid factorisation Pascal's triangle Prime factor Factorization Euler's factorization method Integer factorization Program synthesis Table of Gaussian integer factorizations Unique factorization

Notes

References Fermat (1894), Oeuvres de Fermat, vol. 2, p. 256 McKee, J (1999). "Speeding Fermat's factoring method". Mathematics of Computation. 68 (228): 1729–1737. doi:10.1090/S0025-5718-99-01133-3.

External links Fermat's factorization running time, at blogspot.in Fermat's Factorization Online Calculator, at windowspros.ru

Worked examples

Example 1 — a first encounter with Fermat's factorization method

Start with the simplest possible case. Write down what Fermat's factorization method claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Fermat's factorization method before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Fermat's factorization method ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Fermat's factorization method

In research
Fermat's factorization method appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Fermat's factorization method in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Fermat's factorization method is common in secondary-school and first-year university syllabi. It links to neighbouring topics Integer factorization algorithms, Pierre de Fermat, so understanding it makes those chapters shorter.
In everyday life
Look for Fermat's factorization method outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Fermat's factorization method” →

How to study Fermat's factorization method in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Fermat's factorization method means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Fermat's factorization method out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Fermat's factorization method in simple terms?

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. That difference is algebraically factorable as (a+b)(a−b); if neither factor equals one, it is a proper factorization of N.

Why does Fermat's factorization method matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Fermat's factorization method?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Fermat's factorization method.

Tags

  • Integer factorization algorithms
  • Pierre de Fermat

Keep exploring