Algorithm Search⌘K

Proofs: Lehmer's GCD

These are the human proofs of Lehmer's GCD, at full length.

Lehmer's algorithm returns gcd(u, v) full

This proof shows: correctness.

The input is u≥v≥0u \ge v \ge 0 (the implementation takes absolute values and swaps first), and w≥2w \ge 2. We write β=2w\beta = 2^w. One Euclid step maps a pair (x,y)(x, y) with x≥y>0x \ge y > 0 to (y,x−qy)(y, x - qy) with q=⌊x/y⌋q = \lfloor x/y \rfloor; by the GCD recursion theorem it keeps the gcd.

We prove that each iteration of the outer loop (lines L2 to L10) replaces (u,v)(u, v) by the result of one or more Euclid steps. Then the gcd is kept, the loop ends, and line L11 is Euclid's algorithm.

Setup of one outer iteration. At line L1, v≥βv \ge \beta, and u≥vu \ge v, so uu has at least w+1w + 1 bits and s=bitlength⁡(u)−w≥1s = \operatorname{bitlength}(u) - w \ge 1. Let x=u/2sx = u / 2^s and y=v/2sy = v / 2^s be real numbers. Line L2 sets u^=⌊x⌋\hat u = \lfloor x \rfloor and v^=⌊y⌋\hat v = \lfloor y \rfloor, so x=u^+αx = \hat u + \alpha and y=v^+β′y = \hat v + \beta' with α,β′∈[0,1)\alpha, \beta' \in [0, 1).

Write M=(ABCD)M = \begin{pmatrix} A & B \\ C & D \end{pmatrix} and let (u^,v^)(\hat u, \hat v) denote the current values of the variables. Let (u^0,v^0)(\hat u_0, \hat v_0) be the values set at line L2, and define the virtual pair (u′,v′)=(Au+Bv, Cu+Dv)(u', v') = (A u + B v,\ C u + D v).

Lemma 1 (inner invariant). Before each test of line L4:

  1. (u^,v^)=(Au^0+Bv^0, Cu^0+Dv^0)(\hat u, \hat v) = (A \hat u_0 + B \hat v_0,\ C \hat u_0 + D \hat v_0).
  2. (u′,v′)(u', v') is obtained from (u,v)(u, v) by jj Euclid steps, where jj is the number of completed inner iterations; in particular u′>v′≥0u' > v' \ge 0 if j≥1j \ge 1.
  3. Either A≥0A \ge 0, B≤0B \le 0, C≤0C \le 0, D≥0D \ge 0, or A≤0A \le 0, B≥0B \ge 0, C≥0C \ge 0, D≤0D \le 0.

Proof. Before the first test, MM is the identity, j=0j = 0, and all three hold (the first sign pattern). Assume they hold, the test of line L4 passes, and line L6 does not break, so q≥1q \ge 1 and

q=⌊u^+Av^+C⌋=⌊u^+Bv^+D⌋,v^+C>0,v^+D>0.q = \left\lfloor \frac{\hat u + A}{\hat v + C} \right\rfloor = \left\lfloor \frac{\hat u + B}{\hat v + D} \right\rfloor, \qquad \hat v + C > 0, \quad \hat v + D > 0.

Claim: q=⌊u′/v′⌋q = \lfloor u'/v' \rfloor. By statements 1 and 3 and u′/2s=Ax+Byu' / 2^s = A x + B y,

u′2s−u^=Aα+Bβ′,v′2s−v^=Cα+Dβ′.\frac{u'}{2^s} - \hat u = A \alpha + B \beta', \qquad \frac{v'}{2^s} - \hat v = C \alpha + D \beta'.

In the first sign pattern, Aα∈[0,A]A\alpha \in [0, A] and Bβ′∈[B,0]B\beta' \in [B, 0], so u′/2s∈[u^+B,u^+A]u'/2^s \in [\hat u + B, \hat u + A], and in the same way v′/2s∈[v^+C,v^+D]v'/2^s \in [\hat v + C, \hat v + D]. Let N1=u^+BN_1 = \hat u + B, N2=u^+AN_2 = \hat u + A, V1=v^+CV_1 = \hat v + C, V2=v^+DV_2 = \hat v + D. We have V1>0V_1 > 0, and q≥1q \ge 1 gives N1≥qV2>0N_1 \ge q V_2 > 0. For N∈[N1,N2]N \in [N_1, N_2] and V∈[V1,V2]V \in [V_1, V_2] with all four bounds positive,

N1V2≤NV≤N2V1.\frac{N_1}{V_2} \le \frac{N}{V} \le \frac{N_2}{V_1}.

Both ends have floor qq, so q≤N/V<q+1q \le N/V < q + 1. With N=u′/2sN = u'/2^s and V=v′/2sV = v'/2^s this gives ⌊u′/v′⌋=q\lfloor u'/v' \rfloor = q. In the second sign pattern the roles of (A,C)(A, C) and (B,D)(B, D) swap, and the same argument applies with N1=u^+AN_1 = \hat u + A, N2=u^+BN_2 = \hat u + B, V1=v^+DV_1 = \hat v + D, V2=v^+CV_2 = \hat v + C. Also v′≥2sV1>0v' \ge 2^s V_1 > 0, so the Euclid step on (u′,v′)(u', v') is defined.

Maintenance. Line L7 sets the new matrix to (011−q)M\begin{pmatrix} 0 & 1 \\ 1 & -q \end{pmatrix} M, and line L8 sets (u^,v^)←(v^,u^−qv^)(\hat u, \hat v) \leftarrow (\hat v, \hat u - q \hat v), the same linear map, so statement 1 stays true. The new virtual pair is (v′,u′−qv′)(v', u' - q v'), which by the claim is one Euclid step from (u′,v′)(u', v'); this gives statement 2, and u′−qv′=u′ mod v′<v′u' - qv' = u' \bmod v' < v'. The new rows are (C,D)(C, D) and (A−qC,B−qD)(A - qC, B - qD). In the first pattern, (C,D)(C, D) has C≤0≤DC \le 0 \le D and A−qC≥0≥B−qDA - qC \ge 0 \ge B - qD, which is the second pattern; the second pattern turns into the first in the same way. So statement 3 stays true. □\square

Lemma 2 (one outer iteration). Lines L2 to L10 replace (u,v)(u, v) by a pair obtained from it by at least one Euclid step. The new pair satisfies u≥v≥0u \ge v \ge 0 and has the same gcd.

Proof. The inner loop ends after finitely many iterations, because by Lemma 1 each one is a Euclid step on the positive integers (u′,v′)(u', v'), and v′v' strictly decreases. If it ended with j=0j = 0 steps, MM is the identity and B=0B = 0, and line L9 does one Euclid step on (u,v)(u, v), which is defined since v≥β>0v \ge \beta > 0. If j≥1j \ge 1, then B≠0B \ne 0: after a step, BB is the value of DD before that step, and DD is never 0, because it starts at 1 and each step replaces it by B−qDB - qD with ∣B−qD∣=∣B∣+q∣D∣≥∣D∣|B - qD| = |B| + q|D| \ge |D|, since BB and DD have opposite signs or are 0 by statement 3. Then line L10 sets (u,v)=(u′,v′)(u, v) = (u', v'), which is jj Euclid steps from the old pair by Lemma 1. In both cases the gcd is kept by the GCD recursion theorem, and the new pair has u≥v≥0u \ge v \ge 0. □\square

Theorem. LEHMER-GCD(u,v)(u, v) returns gcd⁡(u,v)\gcd(u, v).

Proof. By Lemma 2, gcd⁡(u,v)\gcd(u, v) is an invariant of the outer loop, and each iteration does at least one Euclid step, so vv strictly decreases while it is positive. Hence the outer loop ends, with v<βv < \beta. Line L11 is Euclid's algorithm on (u,v)(u, v), which ends with v=0v = 0 and u=gcd⁡u = \gcd of the pair at that point; this is the gcd of the input. If the input has v=0v = 0, the outer loop does not run, line L11 does nothing, and u=gcd⁡(u,0)u = \gcd(u, 0); in particular the result for (0,0)(0, 0) is 0. ■\blacksquare

The quotients are the true ones. Lemma 1 shows more than correctness: the quotients that the inner loop finds are exactly the quotients of Euclid's algorithm on (u,v)(u, v), in the same order. So Lehmer's algorithm performs the same sequence of Euclid steps, only grouped, and the Bézout coefficients can be tracked by multiplying the matrices.

Back to Lehmer's GCD