HOME

TheInfoList



OR:

Fermat's factorization method, named after
Pierre de Fermat Pierre de Fermat (; ; 17 August 1601 – 12 January 1665) was a French mathematician who is given credit for early developments that led to infinitesimal calculus, including his technique of adequality. In particular, he is recognized for his d ...
, is based on the representation of an odd
integer An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
as the difference of two squares: :N = a^2 - b^2. That difference is
algebra Algebra is a branch of mathematics that deals with abstract systems, known as algebraic structures, and the manipulation of expressions within those systems. It is a generalization of arithmetic that introduces variables and algebraic ope ...
ically factorable as (a+b)(a-b); if neither factor equals one, it is a proper factorization of ''N''. Each odd number has such a representation. Indeed, if N=cd is a factorization of ''N'', then :N = \left(\frac\right)^2 - \left(\frac\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.


Basic method

One tries various values of ''a'', hoping that a^2-N = b^2, a square. 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, the first try for ''a'' is the square root of rounded up to the next integer, which is . Then b^2 = 78^2-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 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 \cdot 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 - \sqrt N = (\sqrt d - \sqrt c)^2 / 2 = (\sqrt 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 ^ from \sqrt 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 , but also compute ''b'' and throughout. Going up from \sqrt 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 a_ > \sqrt; use Fermat's method for factors between \sqrt and a_. This gives a bound for trial division which is a_ - \sqrt. In the above example, with a_ = 48436 the bound for trial division is 47830. A reasonable choice could be a_ = 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 b^2 are squares: It is not necessary to compute all the square-roots of a^2-N, nor even examine all the values for . Squares are always congruent to 0, 1, 4, 5, 9, 16
modulo In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the '' modulus'' of the operation. Given two positive numbers and , mo ...
20. The values repeat with each increase of by 10. In this example, N is 17 mod 20, so subtracting 17 mod 20 (or adding 3), a^2-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, a^2 must be 1 mod 20, which means that is 1, 9, 11 or 19 mod 20; it will produce a b^2 which ends in 4 mod 20 and, if square, 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: , 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 Recursion occurs when the definition of a concept or process depends on a simpler or previous version of itself. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in m ...
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.


Optimal a_


Premise

An optimal a_ can be computed using derivative methods. The cost of executing Fermat’s method from \sqrt up to a_ is roughly proportional to a constant we will call d. Using sieving we can reduce it by some constant we call l. In the combined method the trial division bound becomes a_ - \sqrt. Writing a_ = \sqrt + d, one gets: a^2_ - N = (\sqrt + d)^2 - N = \sqrt^2 + 2\sqrtd + d^2 - N = 2\sqrtd + d^2 Substitute the new formula, we get a_ - \sqrt \to a_ - \sqrt The goal is to choose a a_ such that C\left(d,N,l\right) = \frac + \left(a_ - \sqrt\right) \to d\frac+ \left(\sqrt+d\right) - \sqrt = \sqrt +d\left(1+\frac\right) - \sqrt is minimized.


Finding the Optimum

Differentiate C\left( d , N,l\right) with respect to d. Due to the linearity of derivatives \frac\sqrt +d\left(1+\frac\right) - \sqrt = \frac\sqrt +\fracd\left(1+\frac\right) - \frac\sqrt Because \sqrt doesn't depend on d we can drop that derivative term for the d\left(1+\frac\right) term, use the multiple rule \left(fg\right)'=f'g+g'f: \left(d\left(1+\frac\right)\right)' = d'\left(1+\frac\right)+\left(1+\frac\right)'d = \left(1+\frac\right) For the last term, we use the chain rule \left(f\left(g\right)\right)' = f'\left(g\right)g' and the power rule x^n = nx^ \frac\sqrt = \frac \frac2\sqrtd + d^2 = \frac \left(2\sqrt + 2d\right) = \left(2\sqrtd + d^2\right)^\left(\sqrt + d\right)Substitute the known derivate formulas \frac\sqrt +\fracd\left(1+\frac\right) - \frac\sqrt = \left(1+\frac\right) - \left(2\sqrtd + d^2\right)^\left(\sqrt + d\right) To find the minimum, notice at the minimum the derivative vanishes, so st the derivative to 0 \left(2\sqrtd + d^2\right)^\left(\sqrt + d\right) = 0 \to \frac = 1+\frac Square both sides to remove the root then cross multiply \left(\frac\right)^2 = \left(1+\frac\right)^2 \to \frac = \left(1+\frac\right)^2 \to \left(\sqrt + d\right)^2 = \left(2\sqrtd + d^2\right)\left(1+\frac\right)^2 Expand LHS \left(\sqrt + d\right)^2 = \left(2\sqrtd + d^2\right)\left(1+\frac\right)^2 \to N + 2\sqrtd+d^2 = \left(2\sqrtd + d^2\right)\left(1+\frac\right)^2 Bring the right side to the left, Factor the common factor, Then, bring the second term to the right-hand side N + 2\sqrtd+d^2 = \left(2\sqrtd + d^2\right)\left(1+\frac\right)^2 \to N = \left(2\sqrtd+d^2\right)\left left(1+\frac\right)^2-1\right Simplify the bracket \left(1+\frac\right)^2-1 = 1^2 + 2\times1\times\frac+\left(\frac\right)^2 - 1 = \frac+\frac = \frac So, the equation is now N = \left(2\sqrtd+d^2\right)\frac \to Nl^2 = \left(2\sqrtd+d^2\right)\left(2l+1\right) To apply the quadratic formula to solve d we have to rewrite the equation to a quadratic. Write the right side as: \left(2\sqrtd+d^2\right)\left(2l+1\right) \to \left(2l+1\right)2\sqrtd+\left(2l+1\right)d^2 Since 2l+1 \neq 0 , you could divide through by it to get d^2 + 2\sqrtd - \frac = 0 This is the quadratic equation we been looking for, we can now apply the quardratic formula: d=\frac \to -\sqrt \pm \sqrt\frac Since d>0 we take the positive solution. Since a_ = \sqrt + d one gets: a_ = \sqrt + d \to \sqrt + -\sqrt + \sqrt\frac = \sqrt\frac


Cost

Substitute our optimal d into C\left( d , N,l\right) \sqrt +d\left(1+\frac\right) - \sqrt \to \sqrt +\left(-\sqrt + \sqrt\frac\right)\left(1+\frac\right) - \sqrt Simplifying the monster of an equation, we get \frac.


Facts

* If l = 1 that means no sieving, a_ = \frac and the cost becomes \sqrt\left(\sqrt-1\right) , which is still better than pure trial division or pure Fermat * The a_ - \sqrt which is the trial division bound becomes \frac when subsututing the optimal.


Example

Using the same N=2345678917, if there's no sieving then you should choose a_ around 55924.69838392813, the reasonable choice a_ = 55000 is not that far off from the optimal with a bound of 28937, but the optimal choice gets a bound of 27962. If we are sieving modulo 20, then l = \frac = 5 and you should choose around \sqrt\frac \approx 87617.1636423325 and this should intuitively make sense. If the Fermat part costs less, the spend more time in the Fermat part to lower a_ - \sqrt


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 In mathematics, a rational number is a number that can be expressed as the quotient or fraction of two integers, a numerator and a non-zero denominator . For example, is a rational number, as is every integer (for example, The set of all ...
v/u can be picked near that value. Nuv = cv \cdot 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(N^) 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 a^2 - 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 In elementary algebra, completing the square is a technique for converting a quadratic polynomial of the form to the form for some values of and . In terms of a new quantity , this expression is a quadratic polynomial with no linear term. By s ...
*
Factorization of polynomials In mathematics and computer algebra, factorization of polynomials or polynomial factorization expresses a polynomial with coefficients in a given field or in the integers as the product of irreducible factors with coefficients in the same doma ...
*
Factor theorem In algebra, the factor theorem connects polynomial factors with polynomial roots. Specifically, if f(x) is a polynomial, then x - a is a factor of f(x) if and only if f (a) = 0 (that is, a is a root of the polynomial). The theorem is a special cas ...
* FOIL rule * Monoid factorisation *
Pascal's triangle In mathematics, Pascal's triangle is an infinite triangular array of the binomial coefficients which play a crucial role in probability theory, combinatorics, and algebra. In much of the Western world, it is named after the French mathematician Bla ...
*
Prime factor A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways ...
*
Factorization In mathematics, factorization (or factorisation, see American and British English spelling differences#-ise, -ize (-isation, -ization), English spelling differences) or factoring consists of writing a number or another mathematical object as a p ...
* Euler's factorization method *
Integer factorization In mathematics, integer factorization is the decomposition of a positive integer into a product of integers. Every positive integer greater than 1 is either the product of two or more integer factors greater than 1, in which case it is a comp ...
*
Program synthesis In computer science, program synthesis is the task to construct a computer program, program that provably correct, provably satisfies a given high-level formal specification. In contrast to program verification, the program is to be constructed rat ...
* Table of Gaussian integer factorizations * Unique factorization


Notes


References

* *


External links


Fermat's factorization running time
at blogspot.in
Fermat's Factorization Online Calculator
at windowspros.ru {{number theoretic algorithms Integer factorization algorithms