The Euclidean Algorithm & GCD
Number Theory & Cryptographic Math
Chapter 4 · The Euclidean Algorithm & GCD
Chapter 3 closed with a gap: division doesn't reduce simply under a modulus, and closing that gap needs a modular inverse. Building a modular inverse needs the extended Euclidean algorithm (Chapter 5) — and that, in turn, is built directly on top of the plain Euclidean algorithm this chapter covers: the single fastest known way to find the greatest common divisor of two numbers.
GCD, and Why the Obvious Approach Doesn't Scale
The greatest common divisor of a and b — gcd(a, b) — is the largest positive integer that divides both. The obvious approach: check every integer from min(a,b) down to 1, stopping at the first one that divides both. That works, but it needs up to min(a,b) checks — for the hundreds-of-digit numbers RSA actually uses, that's not slow, it's completely infeasible, worse than any brute-force approach Algorithms & Complexity ever flagged as impractical.
The Key Identity: gcd(a, b) = gcd(b, a mod b)
This is the entire algorithm, and it's not a shortcut or an approximation — it's an exact equality, provable directly from Chapter 2's own divisibility properties, not asserted on faith.
a = bq + r (Chapter 2's own division algorithm), so r = a mod b. Suppose d divides both a and b. Then d | bq (Chapter 2's multiple property), so d | (a − bq) (Chapter 2's subtraction property) — which is exactly d | r. So every common divisor of (a, b) is also a common divisor of (b, r). Running the same argument in reverse (since a = bq + r also means any common divisor of b and r divides a) shows the two pairs have exactly the same set of common divisors — so they share the same greatest one.
(48, 18): {1, 2, 3, 6}. Common divisors of (18, 48 mod 18 = 12): {1, 2, 3, 6} — identical sets, exactly as the proof above guarantees.
The Algorithm, Traced
Repeatedly replace (a, b) with (b, a mod b) until b reaches 0 — at that point, a is the GCD (the base case: gcd(a, 0) = a).
48 = 18×2 + 12 → next pair (18, 12)18 = 12×1 + 6 → next pair (12, 6)12 = 6×2 + 0 → remainder 0, stop.GCD(48, 18) = 6.
Why It Terminates So Quickly
Each step replaces the larger number with a strictly smaller remainder — Algorithms & Complexity's own recursion-analysis machinery applies directly. It's a provable fact (not covered in depth here, but well established) that the remainder more than halves every two steps in the worst case, giving the algorithm O(log(min(a,b))) steps overall — logarithmic in the size of the input, not linear.
log₂(min(a,b)) ≈ 60.8 — comfortably within the logarithmic bound, for numbers that would take up to 10¹⁹ checks under the naive approach.
gcd(144, 89) takes 10 steps, more than gcd(144, 55)'s 9 steps for a similarly-sized non-Fibonacci pair. But this is still nowhere near slow: consecutive integers are actually one of the fastest cases — gcd(1000, 999) finishes in just 2 steps, since 1000 mod 999 = 1 collapses the problem almost immediately. Even the "worst case" stays logarithmic; there's no input that makes this algorithm slow in the way brute-force factoring (Chapter 1) can be.
Real Relevance
Beyond reducing fractions to lowest terms, this exact algorithm is the direct foundation Chapter 5's extended Euclidean algorithm builds on to compute modular inverses — and RSA key generation itself runs a GCD check (gcd(e, φ(n)) = 1, Chapter 8) to confirm a chosen encryption exponent is actually usable, every single time a key pair is generated.
The Euclidean Algorithm in Code
Hands-On Exercises
Trace the Euclidean algorithm step by step to find gcd(252, 105), showing each a = b×q + r line and the resulting GCD.
Using this chapter's own proof technique (Chapter 2's multiple and subtraction properties), show directly that any common divisor of 84 and 36 must also be a common divisor of 36 and 84 mod 36 — without simply listing out all the divisors of each number.
Trace the Euclidean algorithm for the consecutive Fibonacci pair gcd(55, 34), counting the number of steps, and compare it to gcd(55, 21) (not a consecutive Fibonacci pair, but a similarly-sized input). Which takes more steps, and does this match this chapter's own claim about Fibonacci numbers being a near-worst-case input?
Chapter 4 Quick Reference
- Euclidean algorithm: repeatedly replace
(a,b)with(b, a mod b)untilb=0; the last non-zeroais the GCD - Provably correct:
gcd(a,b) = gcd(b, a mod b), proved directly from Chapter 2's own multiple and subtraction properties - O(log(min(a,b))) steps — dramatically faster than brute-force checking down from
min(a,b) - Consecutive Fibonacci numbers are a genuine near-worst case; consecutive integers are one of the fastest cases
- Direct foundation for Chapter 5's extended Euclidean algorithm and for RSA's own key-generation GCD check
- Next chapter: The extended Euclidean algorithm and modular inverses