Binary GCD algorithm


The binary GCD algorithm, also known as Stein's algorithm, is an algorithm that computes the greatest common divisor of two nonnegative integers. Stein's algorithm uses simpler arithmetic operations than the conventional Euclidean algorithm; it replaces division with arithmetic shifts, comparisons, and subtraction. Although the algorithm was first published by the Israeli physicist and programmer Josef Stein in 1967, it may have been known in 1st-century China.

Algorithm

The algorithm reduces the problem of finding the GCD by repeatedly applying these identities:
For word-sized integers, the algorithm requires a number of machine operations on the order of log.
Asymptotically, the algorithm requires O worst-case time, where n is the number of bits in the larger of the two numbers: although each step reduces at least one of the operands by at least a factor of 2, the subtract and shift operations take linear time.
An extended binary GCD, analogous to the extended Euclidean algorithm, is given by Knuth along with pointers to other versions.

Implementation

Recursive version in C

Following is a recursive implementation of the algorithm in C. The implementation is similar to the description of the algorithm given above. It uses two arguments u and v. All but one of the recursive calls are tail recursive.

unsigned int gcd

Iterative version in C

Following is an implementation of the algorithm in C, taking two integer arguments u and v. It first removes all common factors of 2 using identity 2, then computes the GCD of the remaining numbers using identities 3 and 4, and combines these to form the final answer.

unsigned int gcd

Efficiency

Akhavi and Vallée proved that binary GCD is about 60% more efficient on average, in terms of the number of bit operations, than the Euclidean algorithm.
This algorithm achieves the same asymptotic complexity as the Euclidean algorithm, though neither is the fastest for arbitrary-precision arithmetic, as they both take quadratic time. Instead, recursive methods that combine ideas from the binary GCD algorithm with the Schönhage–Strassen algorithm for fast integer multiplication can find GCDs in near-linear time.

Historical description

An algorithm for computing the GCD of two numbers was described in the ancient Chinese mathematics book The Nine Chapters on the Mathematical Art. The original algorithm was used to reduce a fraction. The description reads:
"If possible halve it; otherwise, take the denominator and the numerator, subtract the lesser from the greater, and do that alternately to make them the same. Reduce by the same number."
This description looks like a normal Euclidean algorithm, but there is ambiguity in the phrase "if possible halve it". The traditional interpretation is that this only applies when 'both' numbers are even, implying the algorithm is just slightly inferior to the Euclidean algorithm. But the phrase may mean dividing by 2 should 'either' of the numbers become even, in which case it is the binary GCD algorithm.