Lehmer's GCD algorithm

Lehmer's GCD algorithm, named after D. H. Lehmer, is a fast GCD algorithm for multiple-precision arithmetic, which improves on the simpler Euclidean algorithm by doing most operations using only the leading digits of the values. "Digit" here is not necessarily a decimal digit; the algorithm is often used with integers represented using a base β such as β = 1000 or β = 232.

Algorithm

Lehmer noted that most of the quotients from each step of the division part of the standard algorithm are small. (For example, Knuth observed that the quotients 1, 2, and 3 comprise 67.7% of all quotients.) Those small quotients can be identified from only a few leading digits. Thus the algorithm starts by splitting off those leading digits and computing the sequence of quotients as long as it is correct.

Say we want to obtain the GCD of the two integers a and b. Let a ≥ b.

References

  1. ↑ Knuth, The Art of Computer Programming vol 2 "Seminumerical algorithms", chapter 4.5.3 Theorem E.