Algorithm Search⌘K

Proofs: Fermat's factorization method

These are the human proofs of Fermat's factorization method, at full length.

Correctness of Fermat's factorization method full

This proof shows: correctness, termination.

Let N≥1N \ge 1 be odd, and let s=⌈N⌉s = \lceil \sqrt N \rceil.

Lemma 1 (pairs and factorizations). The map (u,v)↦(x,y)=(u+v2,v−u2)(u, v) \mapsto (x, y) = \left(\frac{u + v}{2}, \frac{v - u}{2}\right) is a bijection from the pairs of positive integers u≤vu \le v with uv=Nuv = N onto the pairs of integers x>0x > 0, y≥0y \ge 0 with x2−y2=Nx^2 - y^2 = N. Its inverse is (x,y)↦(x−y,x+y)(x, y) \mapsto (x - y, x + y).

Proof. Since NN is odd, uu and vv are odd, so u+vu + v and v−uv - u are even and xx, yy are integers, with x>0x > 0 and y≥0y \ge 0 because v≥u≥1v \ge u \ge 1. Then x2−y2=(x−y)(x+y)=uv=Nx^2 - y^2 = (x - y)(x + y) = uv = N. Conversely, let x>0x > 0, y≥0y \ge 0 and x2−y2=Nx^2 - y^2 = N. Put u=x−yu = x - y and v=x+yv = x + y. Then v>0v > 0, uv=N>0uv = N > 0, so u>0u > 0, and u≤vu \le v because y≥0y \ge 0. The two maps are inverse to each other by direct substitution. □\square

Lemma 2 (order). If uv=Nuv = N with 1≤u≤v1 \le u \le v, then x=(u+v)/2≥sx = (u + v)/2 \ge s. If u<u′u < u' are two divisors of NN that are at most N\sqrt N, with cofactors v=N/uv = N/u and v′=N/u′v' = N/u', then (u′+v′)/2<(u+v)/2(u' + v')/2 < (u + v)/2.

Proof. By the inequality of the arithmetic and geometric means, (u+v)/2≥uv=N(u + v)/2 \ge \sqrt{uv} = \sqrt N, and since (u+v)/2(u + v)/2 is an integer, it is at least ss. For the second claim, (u+v)−(u′+v′)=(u−u′)+N(1u−1u′)=(u′−u)(Nuu′−1)(u + v) - (u' + v') = (u - u') + N\left(\frac1u - \frac1{u'}\right) = (u' - u)\left(\frac{N}{uu'} - 1\right). Here u′−u>0u' - u > 0, and uu′<u′2≤Nuu' < u'^2 \le N, so N/(uu′)>1N/(uu') > 1. The difference is positive. □\square

Invariant. Each time line L3 is reached, r=x2−Nr = x^2 - N, x≥sx \ge s, and no integer x′x' with s≤x′<xs \le x' < x has x′2−Nx'^2 - N equal to a square.

Initialization. Lines L1 and L2 set x=sx = s and r=s2−Nr = s^2 - N. There is no x′x' with s≤x′<ss \le x' < s.

Maintenance. Line L4 runs only when r=x2−Nr = x^2 - N is not a square. It sets rr to x2−N+2x+1=(x+1)2−Nx^2 - N + 2x + 1 = (x + 1)^2 - N and xx to x+1x + 1. The old xx is now one of the values below the new xx, and it failed, so the third part holds again.

Termination. By Lemma 1 with u=1u = 1, v=Nv = N, the value x∗=(N+1)/2x^* = (N + 1)/2 gives x∗2−N=((N−1)/2)2x^{*2} - N = ((N - 1)/2)^2, a square, and x∗≥sx^* \ge s by Lemma 2. The value xx grows by 11 in each pass of the loop, so the loop reaches a square at some x≤x∗x \le x^* and stops after at most x∗−s+1x^* - s + 1 tests.

Correctness. When the loop stops, r=x2−Nr = x^2 - N is a square, and line L5 sets y=r≥0y = \sqrt r \ge 0, so x2−y2=Nx^2 - y^2 = N with x≥s>0x \ge s > 0. By Lemma 1, line L6 returns a pair (a,b)=(x−y,x+y)(a, b) = (x - y, x + y) of positive integers with a≤ba \le b and ab=Nab = N. So a≤Na \le \sqrt N.

It remains to show that aa is the largest divisor of NN at most N\sqrt N. Suppose a divisor u′u' of NN has a<u′≤Na < u' \le \sqrt N, and let x′=(u′+N/u′)/2x' = (u' + N/u')/2. By Lemma 2, s≤x′<(a+b)/2=xs \le x' < (a + b)/2 = x, and by Lemma 1, x′2−Nx'^2 - N is a square. This contradicts the invariant. So aa is the largest such divisor; in particular a=1a = 1 if and only if NN has no divisor in [2,N][2, \sqrt N], that is, if and only if N=1N = 1 or NN is prime.

The number of steps. The loop stops at x=(a+b)/2x = (a + b)/2, so line L4 runs exactly (a+b)/2−s(a + b)/2 - s times. ■\blacksquare

Back to Fermat's factorization method