← LibraryLehmer's Accelerated GCD ComputationEngineering · MathematicsLesson 216/385← PrevNext →
ArticlePublished 7 Aug 20262 min readBy Kevin JoginLehmer GCDaccelerationsingle precisionquotient sequence

Euclidean Algorithms and Congruences

Lehmer's Accelerated GCD Computation

Lehmer's method: running many GCD steps on single-precision leading digits before touching the full multiprecision operands.

Engineering / MathematicsEuclidean Algorithms and Congruences2 min readKV-MATH-0514

Lehmer's observation is that the sequence of quotients in the Euclidean algorithm usually depends only on the leading digits of the operands. Many steps can therefore be simulated in single precision and applied to the full operands in one batch.

The idea

Take the leading limbs of both operands. Run the Euclidean algorithm on those single-precision values, accumulating the transformation as a two-by-two integer matrix, and stop as soon as the quotient computed from the leading digits might differ from the true quotient.

(a, b) -> (A a + B b, C a + D b)The matrix entries stay small; the multiprecision update happens once for many steps.

Lehmer's accelerated GCD

  1. ExtractTake the top limb of the larger operand and the corresponding limb of the smaller.
  2. SimulateRun Euclidean steps on the single-precision pair, updating the transformation matrix.
  3. GuardStop when the quotient from the leading digits is no longer provably correct — a bound test on the matrix entries.
  4. ApplyApply the accumulated matrix to the full operands with multiprecision arithmetic.
  5. RepeatContinue until the operands fit in single precision.

Why it wins

The guard condition

Relationship to other methods

GCD methods compared
MethodApproachComplexity
ClassicalOne division per stepO(n^2) bit operations
BinaryShifts and subtractionsO(n^2), smaller constant
LehmerBatched single-precision simulationO(n^2), much smaller constant
Half-GCDDivide and conquerO(M(n) log n)

Lehmer's method does not improve the asymptotic exponent — it reduces the constant substantially, which is why it remains the workhorse in the range where most computation actually happens. The same batching idea extends to the extended algorithm; see the extended algorithm.

Source. Henri Cohen, A Course in Computational Algebraic Number Theory, Springer GTM 138 — 1.3.1. Structural reference unverified: the source file was not available during authoring; chapter and section numbers are taken from the published edition and have not been checked against a physical copy.

Continue learning

The Euclidean Algorithm: Classical and Binary VariantsArticle · MathematicsNEXT LESSON →The Extended Euclidean Algorithm and Bezout CoefficientsArticle · MathematicsInteger Square Root and Perfect Power DetectionArticle · MathematicsChinese Remainder Theorem AlgorithmsArticle · Mathematics