elementary number theory
The Euclidean Algorithm
You should know: divisibility, gcd and lcm
Overview
The Euclidean algorithm is one of the oldest algorithms still in use — it appears in Euclid's Elements (c. 300 BCE) — and computes the greatest common divisor (GCD) of two integers without factoring either of them. Its engine is a single fact: any common divisor of a and b also divides their difference, and more sharply, gcd(a, b) = gcd(b, a mod b). Repeatedly replacing the larger number by the remainder of dividing it by the smaller shrinks the pair quickly; when the remainder hits 0, the last nonzero remainder is the GCD. Because the numbers fall at least geometrically, the algorithm runs in O(log(min(a, b))) division steps — dramatically faster than listing divisors or factoring, which is why it underlies modular inverses, RSA key setup, and continued fractions.
Intuition
Picture tiling an a-by-b rectangle with the largest squares possible. Lay down as many b-by-b squares as fit; a strip of width (a mod b) is left over. Now tile THAT strip the same way. Each leftover strip is smaller than the last, so the process must stop — and the side of the final square that tiles everything exactly is gcd(a, b). The algorithm is just this geometric peeling written arithmetically.
Formal Definition
For integers a ≥ b > 0, define the remainder sequence r₀ = a, r₁ = b, and rₖ₊₁ = rₖ₋₁ mod rₖ until a zero remainder appears:
Worked Examples
Divide 252 by 105; the remainder replaces the larger number.
Now reduce the pair (105, 42).
Reduce (42, 21); the remainder is 0, so the last nonzero remainder, 21, is the GCD.
Answer: gcd(252, 105) = 21.
Practice Problems
Compute 105 mod 42.
Compute 1071 mod 462.
Two gears with 1071 and 462 teeth mesh. Using the Euclidean algorithm, how many teeth apart is the largest tooth-spacing that both gears share (their GCD)?
Quiz
Summary
- gcd(a, b) = gcd(b, a mod b): repeatedly reduce the larger number modulo the smaller.
- The last nonzero remainder is the GCD; the algorithm runs in O(log min(a,b)) division steps.
- Extended to track coefficients, it yields Bézout's identity ax + by = gcd(a,b) and hence modular inverses.
Mathematics