GCD, LCM & the Euclidean Algorithm

Master the oldest algorithm in mathematics: computing GCD through repeated division and Bézout's identity

The Oldest Algorithm

The Euclidean algorithm, described by Euclid around 300 BC, is the oldest known algorithm still in widespread use. It computes the greatest common divisor (GCD) of two integers through repeated division — and it has a beautiful geometric interpretation.

Beyond computing GCD, the extended Euclidean algorithm finds integers x and y satisfying ax + by = gcd(a, b) (Bézout's identity). This result is foundational for modular arithmetic, cryptography, and Diophantine equations.

Demo 1: Euclidean Algorithm Visualizer

Watch the algorithm in action: at each step, divide the larger number by the smaller, keep the remainder, and repeat. The geometric interpretation shows this as tiling a rectangle with squares — the GCD is the side length of the smallest square that tiles the original rectangle.

Euclidean Algorithm Stepper

Stepa=q × b+r
1252=2 × 105+42
2105=2 × 42+21
342=2 × 21+0

Rectangle Tiling

Demo 2: Bézout's Identity Finder

The extended Euclidean algorithm goes further: it finds integers x and y such that ax + by = gcd(a, b). Watch the forward pass compute the GCD, then the back-substitution recover the Bézout coefficients.

Bézout Identity Finder

Forward pass

Euclidean Algorithm

StepEquation
1240 = 5 × 46 + 10
246 = 4 × 10 + 6
310 = 1 × 6 + 4
46 = 1 × 4 + 2
54 = 2 × 2 + 0

Demo 3: GCD and LCM Relationship

The GCD and LCM are two sides of the same coin. Through prime factorizations, GCD takes the minimum exponent of each prime while LCM takes the maximum. Together they satisfy the beautiful identity: gcd(a,b) × lcm(a,b) = a × b.

GCD & LCM Relationship

36 = 2^2 × 3^2
48 = 2^4 × 3
Only a
--
Shared (GCD)
2^23
Only b
--

gcd(36, 48)

12

lcm(36, 48)

144

gcd(36, 48) × lcm(36, 48) = 12 × 144 = 1728

36 × 48 = 1728

Verified: gcd(a,b) × lcm(a,b) = a × b

Demo 4: GCD as Lattice Points

Here's a surprising geometric fact: draw a line segment from (0,0) to (a,b) on an integer grid. The number of lattice points on the segment (including endpoints) is exactly gcd(a,b) + 1. Drag the endpoint to explore!

GCD & Lattice Points

gcd(12, 8) = 4
Lattice points on line = 5

The number of lattice points on the segment from (0,0) to (a,b), including endpoints, equals gcd(a,b) + 1. Drag the orange endpoint to move it.

0055101015152020(0,0)(12,8)

GCD Mastered!

You've learned the fundamental tools of divisibility computation:

  • The Euclidean algorithm and its geometric rectangle-tiling interpretation
  • Bézout's identity and the extended Euclidean algorithm
  • The deep connection between GCD and LCM through prime factorizations
  • The geometric meaning of GCD as lattice point counting

Next: We enter the world of modular arithmetic — where numbers wrap around like a clock, and entirely new algebraic structures emerge.