GCD Formula – Greatest Common Divisor Euclidean Algorithm

The complete GCD formula using the Euclidean algorithm: variable definitions, step-by-step method, and three fully worked examples.

Formula
The greatest common divisor of two positive integers a and b is computed by the Euclidean algorithm: replace (a, b) with (b, a mod b) repeatedly until the second argument is zero. The GCD is the final non-zero value.
Variables
SymbolNameDescriptionUnit
aFirst integerA positive integer; the larger of the two initially (the algorithm works regardless of order)
bSecond integerA positive integer; replaced by a mod b at each step
qQuotientq = ⌊a / b⌋, the integer quotient at each step
rRemainderr = a mod b = a − b × q; this becomes the new b at the next step
GCDGreatest Common DivisorThe final non-zero remainder; the largest integer dividing both original inputs
How to Use
  1. If b = 0, return a as the GCD.
  2. Compute the remainder: r = a mod b.
  3. Set a ← b and b ← r.
  4. Repeat from step 1.
Examples
1. GCD(48, 36)

Apply the Euclidean algorithm step by step.

Stepaba = b × q + rRemainder r
1483648 = 36 × 1 + 1212
2361236 = 12 × 3 + 00
Verification: 48 ÷ 12 = 4, 36 ÷ 12 = 3. Both divide exactly. No larger divisor exists.
2. GCD(1071, 462)

A larger example to show multiple Euclidean steps.

Stepaba = b × q + rRemainder r
110714621071 = 462 × 2 + 147147
2462147462 = 147 × 3 + 2121
314721147 = 21 × 7 + 00
1071 = 3 × 357 = 3 × 3 × 7 × 17 = 3² × 7 × 17; 462 = 2 × 3 × 7 × 11. Shared factors: 3 × 7 = 21. ✓
3. GCD(360, 756) via prime factorization comparison

Show the prime factorization method as an alternative.

For each prime, take the minimum exponent:

PrimeExp in 360Exp in 756min
2322
3232
5100
7010
💡Both methods give 36. The Euclidean algorithm is faster in practice; prime factorization is more visual for understanding why the answer is what it is.