KEVOS
ArticlesServicesCase studiesAboutContact
ArticlesServicesCase studiesAboutContact
← ArticlesThe Euclidean Algorithm: Classical and Binary VariantsEngineering · Engineering MathematicsLesson 714/884← PrevNext →
ArticlePublished 7 Aug 20262 min readBy Kevin JoginEuclidean algorithmGCDbinary GCDStein algorithm
On this page

Ask about this page

KEVOS AIThe Euclidean Algorithm: Classical and Binary Variants

KEVOS knowledge first · trusted web sources when needed

Euclidean Algorithms and Congruences

The Euclidean Algorithm: Classical and Binary Variants

The classical Euclidean algorithm, the binary variant that replaces division with shifts, and how to choose between them.

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

The greatest common divisor is the single most frequently invoked operation in computational number theory. Two algorithms dominate, and they trade a small number of expensive steps against a large number of cheap ones.

The classical algorithm

Repeatedly replace the pair by the smaller element and the remainder of the division. The process terminates because the remainder strictly decreases.

gcd(a, b) = gcd(b, a mod b), gcd(a, 0) = aEach step requires one multiprecision division.

Key point

The number of steps is O(log min(a,b)), with the worst case given by consecutive Fibonacci numbers. This bound is tight and is the reason the algorithm is fast despite its simplicity.

The binary algorithm

Stein's variant eliminates division entirely, using only subtraction, comparison and shifts. It rests on three observations.

  • If both are even, the GCD is twice the GCD of the halves.
  • If one is even and the other odd, the factor of two cannot divide the GCD, so it may be discarded.
  • If both are odd, their difference is even and smaller than the larger of the two.

Binary GCD

  1. Extract common twosRemove the largest power of two dividing both; remember it for the end.
  2. Make both oddShift out factors of two from each operand individually.
  3. Subtract and shiftReplace the larger by the difference, then shift out its factors of two.
  4. RepeatContinue until one operand is zero.
  5. RestoreShift the result left by the common power of two.

Choosing between them

Classical versus binary GCD
CriterionClassicalBinary
Operations per stepOne divisionSubtraction and shifts
Number of stepsFewerMore, roughly proportional to bits
Small operandsCompetitiveUsually faster
Large operandsBetter, division amortisesSubtraction count grows
Hardware without fast divisionPoorStrong

Note

For very large operands neither is optimal. The half-GCD method achieves O(M(n) log n) by a divide-and-conquer scheme, but its overhead means it only pays above several hundred limbs.

Where GCD appears

The operation is ubiquitous: reducing fractions, testing coprimality, detecting factors in Pollard rho and ECM, computing polynomial content, and as the inner loop of Hermite normal form.

Frequently Asked Questions

Why is the Fibonacci case the worst case?
Because consecutive Fibonacci numbers produce a quotient of one at every step, which is the slowest possible reduction. Any larger quotient shrinks the operands faster.

Source. Henri Cohen, A Course in Computational Algebraic Number Theory, Springer GTM 138 — 1.3.1-1.3.2. 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.

Related pages

  • Learning Pathways Through Computational Number Theory
  • The Polynomial Euclidean Algorithm over a Field
  • Lehmer's Accelerated GCD Computation

Continue learning

Integer Square Root and Perfect Power DetectionArticle · Engineering MathematicsNEXT LESSON →Lehmer's Accelerated GCD ComputationArticle · Engineering MathematicsBinary Powering and Exponentiation ChainsArticle · Engineering MathematicsThe Extended Euclidean Algorithm and Bezout CoefficientsArticle · Engineering Mathematics
KEVOS · Engineering, manufacturing and project improvement
ArticlesServicesCase studiesAboutContact
© 2026 KEVOS®