Algorithm Search⌘K

Proofs: Half-GCD

These are the human proofs of Half-GCD, at full length.

HGCD returns the Euclid state straddling 2^m, and GCD returns gcd(a, b) full

This proof shows: correctness.

Write E(q)=(q110)E(q) = \begin{pmatrix} q & 1 \\ 1 & 0 \end{pmatrix} for the quotient matrix of qq. Then E(q)(x,y)T=(qx+y,x)TE(q) (x, y)^T = (qx + y, x)^T, det⁡E(q)=−1\det E(q) = -1 and E(q)−1=(011−q)E(q)^{-1} = \begin{pmatrix} 0 & 1 \\ 1 & -q \end{pmatrix}. One Euclid step maps (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; equivalently (x,y)T=E(q)(y,x−qy)T(x, y)^T = E(q) (y, x - qy)^T. For an input a≥b≥0a \ge b \ge 0 of HGCD, the Euclid states are the pairs s0=(a,b)s_0 = (a, b), s1s_1, s2s_2, ..., obtained by Euclid steps until the second entry is 0, and QiQ_i is the list of the first ii quotients. The second entries strictly decrease.

Lemma 1 (certificate). Let q1,…,qj≥1q_1, \ldots, q_j \ge 1 with j≥1j \ge 1, R=E(q1)⋯E(qj)R = E(q_1) \cdots E(q_j), and (α,β)T=R−1(a,b)T(\alpha, \beta)^T = R^{-1} (a, b)^T. If α>β>0\alpha > \beta > 0, then (α,β)=sj(\alpha, \beta) = s_j and the first jj quotients of Euclid on (a,b)(a, b) are q1,…,qjq_1, \ldots, q_j.

Proof. Let pj=(α,β)p_j = (\alpha, \beta) and pi−1=E(qi)pip_{i-1} = E(q_i) p_i for i=j,…,1i = j, \ldots, 1, so p0=Rpj=(a,b)p_0 = R p_j = (a, b). We show by downward induction that pi=(xi,yi)p_i = (x_i, y_i) satisfies xi>yi>0x_i > y_i > 0 for all ii. It holds for i=ji = j. If it holds for ii, then pi−1=(qixi+yi,xi)p_{i-1} = (q_i x_i + y_i, x_i), and qixi+yi≥xi+yi>xi>0q_i x_i + y_i \ge x_i + y_i > x_i > 0, so it holds for i−1i - 1. Now for each i≥1i \ge 1, pi−1=(qixi+yi,xi)p_{i-1} = (q_i x_i + y_i, x_i) with 0<yi<xi0 < y_i < x_i, so ⌊(qixi+yi)/xi⌋=qi\lfloor (q_i x_i + y_i)/x_i \rfloor = q_i and the remainder is yiy_i: the Euclid step from pi−1p_{i-1} has quotient qiq_i and leads to pip_i. Since p0=(a,b)=s0p_0 = (a, b) = s_0, induction on ii gives pi=sip_i = s_i with quotients q1,…,qiq_1, \ldots, q_i. □\square

Lemma 2 (FIXUP). Let R=E(q1)⋯E(qj)R = E(q_1) \cdots E(q_j) with all qi≥1q_i \ge 1 and P=(q1,…,qj)P = (q_1, \ldots, q_j), j≥0j \ge 0, and let a≥b≥0a \ge b \ge 0 and L≤aL \le a. Then FIXUP(R,P,a,b,L)(R, P, a, b, L) terminates and returns (R′,P′,α,β)(R', P', \alpha, \beta) with P′P' a prefix (q1,…,qi)(q_1, \ldots, q_i) of PP, R′=E(q1)⋯E(qi)R' = E(q_1) \cdots E(q_i), (a,b)T=R′(α,β)T(a, b)^T = R' (\alpha, \beta)^T, and either i=0i = 0 and (α,β)=(a,b)(\alpha, \beta) = (a, b), or i≥1i \ge 1, α>β>0\alpha > \beta > 0 and α≥L\alpha \ge L. In both cases (α,β)=si(\alpha, \beta) = s_i and P′=QiP' = Q_i.

Proof. Line L11 sets (α,β)T=R−1(a,b)T(\alpha, \beta)^T = R^{-1}(a, b)^T; the implementation computes R−1R^{-1} as det⁡R\det R times the adjugate, which is exact because det⁡R=(−1)j\det R = (-1)^j. The loop keeps the relation (α,β)T=R−1(a,b)T(\alpha, \beta)^T = R^{-1} (a, b)^T with RR the product over PP: removing qq replaces RR by RE(q)−1R E(q)^{-1}, whose inverse is E(q)R−1E(q) R^{-1}, and E(q)(α,β)T=(qα+β,α)TE(q) (\alpha, \beta)^T = (q\alpha + \beta, \alpha)^T, which is the update of line L12. Each iteration shortens PP, so the loop ends. If it ends with PP empty, then R=IR = I and (α,β)=(a,b)=s0(\alpha, \beta) = (a, b) = s_0. Otherwise it ends because α>β>0\alpha > \beta > 0 and α≥L\alpha \ge L, and Lemma 1 gives (α,β)=si(\alpha, \beta) = s_i with quotients P′P'. □\square

Lemma 3 (HGCD). For a≥b≥0a \ge b \ge 0 and a threshold T≥2T \ge 2, HGCD(a,b)(a, b) terminates and returns (M,Q,α,β)(M, Q, \alpha, \beta) with Q=QjQ = Q_j, M=E(q1)⋯E(qj)M = E(q_1) \cdots E(q_j), (α,β)=sj(\alpha, \beta) = s_j, and α≥2m>β\alpha \ge 2^m > \beta, where m=⌈n/2⌉m = \lceil n/2 \rceil and nn is the bit length of aa; or j=0j = 0 and b<2mb < 2^m.

Proof. By strong induction on nn. Let λ=2m\lambda = 2^m.

Lines L1 to L3. If b<λb < \lambda, line L2 returns s0s_0 with j=0j = 0, as claimed. If n≤Tn \le T, line L3 does Euclid steps while the second entry is at least λ\lambda, and records each quotient; this is the claim directly, since the second entries decrease, and each step is legal because the second entry is positive.

Otherwise n>T≥2n > T \ge 2, so m≥1m \ge 1, and b≥λb \ge \lambda. We use the invariant: the current pair is sis_i for i=∣Q∣i = |Q|, Q=QiQ = Q_i, M=E(q1)⋯E(qi)M = E(q_1) \cdots E(q_i), and the first entry of the current pair is at least λ\lambda.

Line L4. The arguments ⌊a/λ⌋≥⌊b/λ⌋≥0\lfloor a/\lambda \rfloor \ge \lfloor b/\lambda \rfloor \ge 0 have n−m<nn - m < n bits, so by the induction hypothesis the call terminates and returns quotients PP, all at least 1, and RR their product. (Its α\alpha and β\beta are not used.)

Line L5. By Lemma 2 with L=λ≤aL = \lambda \le a, the result is sis_i with Q=QiQ = Q_i and MM their product, and its first entry is at least λ\lambda: either it is a≥λa \ge \lambda (i=0i = 0), or it was checked. So the invariant holds.

Line L6. If b≥λb \ge \lambda, one Euclid step is legal (b>0b > 0), gives si+1s_{i+1} and appends its quotient; the new first entry is the old b≥λb \ge \lambda. The invariant holds.

Lines L7 and L8. Let ℓ\ell be the bit length of the current aa. The call is made only if b≥λb \ge \lambda and 2(ℓ−m)<n2(\ell - m) < n. Since ℓ≤n≤2m\ell \le n \le 2m, k=2m−ℓ≥0k = 2m - \ell \ge 0, and the arguments ⌊a/2k⌋≥⌊b/2k⌋≥0\lfloor a/2^k \rfloor \ge \lfloor b/2^k \rfloor \ge 0 have ℓ−k=2(ℓ−m)<n\ell - k = 2(\ell - m) < n bits, so the induction hypothesis applies again. Lemma 2 for the current pair sis_i, with L=λ≤aL = \lambda \le a, gives the Euclid state si+i′s_{i+i'} of the current pair after i′i' more quotients, which are the next quotients of the original input. Multiplying MM by SS and appending PP keeps the invariant.

Line L9. Each Euclid step is legal because b≥λ>0b \ge \lambda > 0, and it keeps the invariant because the new first entry is the old b≥λb \ge \lambda. The loop ends because the second entries strictly decrease.

Line L10. Now b<λb < \lambda and a≥λa \ge \lambda by the invariant. The current pair is sjs_j with j=∣Q∣j = |Q|, and the second entries of the Euclid states decrease, so sjs_j is the unique state with first entry at least λ\lambda and second entry below λ\lambda. □\square

Theorem. GCD(a,b)(a, b) returns gcd⁡(a,b)\gcd(a, b) for all integers aa and bb.

Proof. Line L14 replaces a,ba, b by their absolute values in decreasing order, which does not change the gcd. Each pass of line L15 replaces (a,b)(a, b) by a Euclid state of it (Lemma 3), and then, if b≠0b \ne 0, by one more Euclid step. Euclid steps keep the gcd by the GCD recursion theorem: gcd⁡(x,y)=gcd⁡(y,x−qy)\gcd(x, y) = \gcd(y, x - qy). The pass strictly decreases bb: HGCD does not increase it, and the final step replaces bb by a mod b<ba \bmod b < b. So line L15 ends, and the order a≥ba \ge b is kept. Line L16 is Euclid's algorithm, which ends with (gcd⁡,0)(\gcd, 0), and gcd⁡(x,0)=x\gcd(x, 0) = x for x≥0x \ge 0. With a=b=0a = b = 0, no loop runs and the result is 0=gcd⁡(0,0)0 = \gcd(0, 0). ■\blacksquare

Back to Half-GCD