TAOCP 4.5.2 Exercise 39

Algorithm B is the binary gcd algorithm.

Section 4.5.2: The Greatest Common Divisor

Exercise 39. ▶ [M28] (V. R. Pratt.) Extend Algorithm B to an Algorithm Y that is analogous to Algorithm X.

Verified: yes
Solve time: 1m12s


Setup

Algorithm B is the binary gcd algorithm. Algorithm X, introduced earlier in the section, augments Euclid's algorithm so that, together with

$$$$

it also produces integers $a,b$ satisfying

$$$$

The exercise asks for an analogous extension of Algorithm B. We therefore seek an algorithm Y that follows the same sequence of binary gcd transformations while maintaining coefficients that express every current quantity as an integral linear combination of the original inputs.

Let the original positive integers be denoted by $u$ and $v$. During the computation we maintain numbers $U$ and $V$ together with integers

$$$$

such that

$$$$

Initially,

$$$$

hence

$$$$

The invariant (1) will be preserved throughout.

Solution

Algorithm B begins by removing common powers of $2$. Let

$$$$

where at least one of $u_1,v_1$ is odd. The factor $2^e$ is saved separately and never affects the coefficient calculations. Thus the coefficient computation may be carried out for $u_1$ and $v_1$; the final gcd is multiplied by $2^e$.

Assume that $U$ is even. In Algorithm B, $U$ is replaced by $U/2$. Since

$$$$

the coefficients must be modified so that the representation (1) remains valid after division by $2$.

If both $A$ and $B$ are even, set

$$$$

Then

$$$$

If $A$ and $B$ are not both even, replace them by

$$ B\leftarrow \frac{B-u}{2}. \tag{2} $$

Since

$$ +\left(\frac{B-u}{2}\right)v =\frac{Au+Bv}{2} =\frac U2, $$

the invariant is preserved.

The parity needed for (2) is guaranteed. Indeed, $U=Au+Bv$ is even. Since at least one of $u,v$ is odd after the common factor $2^e$ has been removed, $A$ and $B$ have opposite parity whenever they are not both even. Consequently $A+v$ and $B-u$ are both even, so the divisions by $2$ are legitimate.

A completely symmetric rule is used whenever $V$ is even:

$$ D\leftarrow \frac{D-u}{2}, $$

unless $C$ and $D$ are already both even, in which case they are simply halved.

The subtraction step of Algorithm B requires no special work. If

$$$$

Algorithm B replaces $U$ by $U-V$. Using (1),

$$$$

Hence we set

$$ B\leftarrow B-D. $$

Similarly, when

$$$$

we replace

$$ D\leftarrow D-B. $$

The invariant (1) is again preserved.

Algorithm B terminates with

$$$$

where

$$$$

At that moment,

$$$$

Multiplying by the common power $2^e$ removed at the beginning gives

$$ =(2^eA)u+(2^eB)v. $$

Thus Algorithm Y returns both

$$$$

and coefficients

$$$$

satisfying

$$$$

This is exactly the binary analogue of Algorithm X.

Verification

The invariant

$$$$

holds initially.

For each halving operation, either both coefficients are halved directly, or the replacement (2) is used. In both cases the new coefficients represent the new value $U/2$. The same argument applies to $V$.

For each subtraction operation,

$$$$

or symmetrically

$$$$

so the invariant is preserved.

Since Algorithm Y performs exactly the same arithmetic operations on $U$ and $V$ as Algorithm B, it terminates whenever Algorithm B terminates and reaches the same gcd. The accompanying coefficients therefore satisfy Bézout's identity for the original inputs.

This completes the proof.

Notes

Algorithm Y is the binary, or Stein-type, counterpart of the extended Euclidean algorithm. Its output consists of a gcd together with Bézout coefficients, just as Algorithm X extends Euclid's algorithm by maintaining linear-combination representations throughout the computation.