← LibraryEuclid's Algorithm for Integer GCDEngineering · MathematicsLesson 55/385← PrevNext →
ArticlePublished 7 Aug 20263 min readBy Kevin Jogin

Engineering  /  Mathematics  — Integer Algorithms

Euclid's Algorithm for Integer GCD

Euclid's algorithm for greatest common divisors, its correctness, and the Fibonacci worst case that bounds its iteration count.

Page KV-MATH-0326Reading time 3 minReviewed 2026-08-07Author Kevin Jogin

Executive summary

Euclid's algorithm computes the gcd by repeatedly replacing the larger argument with its remainder modulo the smaller. It is among the oldest algorithms still in use and remains the method of choice.

Its efficiency is not obvious from the statement. The worst case is attained by consecutive Fibonacci numbers, and the iteration count is logarithmic in the operands.

Learning objectives

  1. State the algorithm and prove correctness from the gcd shift identity.
  2. Bound the number of iterations.
  3. Identify the Fibonacci worst case.

01The algorithm and its correctness

Algorithm

Euclidean algorithm

Inputnon-negative integers a, b
Outputgcd(a, b)
  1. Given a ≥ b ≥ 0.
  2. While b ≠ 0:
  3.   Compute r = a mod b.
  4.   Set a = b, b = r.
  5. Return a.
Cost  O(len(a) · len(b)) bit operations
Theorem

Correctness

gcd(a, b) = gcd(b, a mod b).

Reason. Any common divisor of a and b divides a − qb = a mod b, and any common divisor of b and a mod b divides qb + (a mod b) = a. The two pairs have identical common divisors, hence the same greatest one.

Termination is immediate: the second argument strictly decreases and stays non-negative, so the loop cannot run forever. The remaining question is how fast it decreases.

02The iteration bound

Theorem

Lamé's theorem

The number of division steps is at most about 4.8 · log₁₀(min(a,b)), and the worst case occurs when the inputs are consecutive Fibonacci numbers.

The mechanism is that each pair of consecutive steps at least halves the larger argument. If r = a mod b then either r < b/2 directly, or r ≥ b/2, in which case the next remainder is b mod r = b − r < b/2. Either way the value halves within two steps, giving a logarithmic bound.

03Variants

Euclidean algorithm variants
VariantIdeaWhen preferable
ClassicalDivision with remainder each stepDefault; fewest iterations
Binary gcdRemove factors of 2, subtract, halveNo division needed; good where division is costly
LehmerOperate on leading digits to batch several stepsLarge multiprecision operands
ExtendedTrack Bezout coefficients alongsideWhen the coefficients are needed

The binary variant replaces division with subtraction and shifting, which on some architectures is faster despite performing more iterations. Lehmer's variant is the standard choice for very large operands, batching many small-quotient steps using single-word arithmetic on the leading digits.

04Frequently asked questions

Why is the bit complexity quadratic rather than logarithmic?

Because there are O(len) iterations and each performs a division costing O(len²) in the worst case, but the total is O(len²) rather than O(len³) — the operands shrink, and summing the actual per-step costs telescopes. The careful analysis gives O(len(a) · len(b)).

Does the algorithm work for negative inputs?

After taking absolute values, yes. The gcd is defined to be non-negative, so the standard approach normalises signs on entry.

Is there anything asymptotically faster?

Yes. A divide-and-conquer variant based on the same idea as Lehmer's achieves essentially the cost of multiplication times a logarithmic factor, and is used in libraries for very large inputs. It is considerably more intricate.

Sources and method

Structural reference: Victor Shoup, A Computational Introduction to Number Theory and Algebra, Version 1, Cambridge University Press, 2005 — book pages 55-58.

This page carries the durable method layer only: definitions, constructions, algorithms, complexity results and selection criteria, authored originally for KEVOS. No text is transcribed or paraphrased from the source, and no numeric tables or benchmark data are reproduced — these are routed to live authoritative sources instead.

Author: Kevin Jogin. Last reviewed 2026-08-07.

Continue learning

Faster Integer Arithmetic: Karatsuba and BeyondArticle · MathematicsNEXT LESSON →The Extended Euclidean AlgorithmArticle · MathematicsModular Exponentiation by Repeated SquaringArticle · MathematicsModular Inverses and Chinese RemainderingArticle · Mathematics