Sub topic 3
Why gcd(a, b) Equals gcd(b, a mod b)
This chapter assumes you know what a divisor and the greatest common divisor are, and that you can write a = q·b + r with 0 ≤ r < |b| (division with remainder). Read the proof below carefully. Euclid's algorithm is correct only because of this one identity.
The claim
For integers a and b with b ≠ 0, let r = a mod b. Then
gcd(a, b) = gcd(b, r)
This rule replaces the pair (a, b) with a smaller pair (b, r) that has the same GCD. Euclid's algorithm does nothing more than apply it repeatedly.
The key idea: the common divisors are identical
The proof does not compare the two GCDs directly. It shows something stronger: the pair (a, b) and the pair (b, r) have exactly the same set of common divisors. If two sets are equal, their largest elements are equal too, so the GCDs match.
Division with remainder gives us integers q and r with:
a = q·b + r, which rearranges to r = a − q·b
Direction 1: every common divisor of a and b divides r
Suppose d divides both a and b. Then a = d·m and b = d·n for some integers m and n. Substituting:
r = a − q·b = d·m − q·d·n = d·(m − q·n)
Since m − q·n is an integer, d divides r. So d is a common divisor of b and r.
Direction 2: every common divisor of b and r divides a
Suppose d divides both b and r, with b = d·n and r = d·k. Then:
a = q·b + r = q·d·n + d·k = d·(q·n + k)
So d divides a, which makes it a common divisor of a and b.
Conclusion
The two directions together show that the common divisors of (a, b) are exactly the common divisors of (b, r). Equal sets have equal maximums, so gcd(a, b) = gcd(b, r). ∎
A concrete check
Take a = 1071 and b = 462. Since 1071 = 2·462 + 147, we have r = 147.
| Pair | Factorization | Common divisors | GCD |
|---|---|---|---|
| (1071, 462) | 3²·7·17 and 2·3·7·11 | 1, 3, 7, 21 | 21 |
| (462, 147) | 2·3·7·11 and 3·7² | 1, 3, 7, 21 | 21 |
Both pairs have the same common divisor set, as the proof predicts.
You can also check this by brute force. The script below compares the full common-divisor sets, not only the GCDs:
import random
def common_divisors(x, y):
limit = min(abs(x), abs(y))
return {d for d in range(1, limit + 1) if x % d == 0 and y % d == 0}
def check(a, b):
r = a % b
if r == 0:
# gcd(b, 0) = b; every divisor of b divides 0
return common_divisors(a, b) == {d for d in range(1, b + 1) if b % d == 0}
return common_divisors(a, b) == common_divisors(b, r)
print(check(1071, 462)) # True
random.seed(1)
pairs = [(random.randint(1, 2000), random.randint(1, 2000)) for _ in range(500)]
print(all(check(a, b) for a, b in pairs)) # TrueCommon mistakes
- Swapping the order. The new pair is
(b, a mod b), not(a, a mod b). Keepingawould make no progress, becausea mod bis smaller thanb, not smaller thana. - Forgetting the
b ≠ 0condition.a mod 0is undefined. The caseb = 0is the stopping point, wheregcd(a, 0) = |a|, because every integer divides 0. - Arguing only one direction. Showing that divisors of
aandbdividerproves only that the new common divisors include the old ones. You need both directions to conclude that the two sets are equal.
Exercise
Verify the identity for a = 84 and b = 36. Compute r = 84 mod 36, list the common divisors of (84, 36) and of (36, r), and state both GCDs.
Show answer
84 = 2·36 + 12, so r = 12. The common divisors of (84, 36) are 1, 2, 3, 4, 6, 12. The common divisors of (36, 12) are also 1, 2, 3, 4, 6, 12. Both GCDs are 12.
Exercise
The proof still works if you replace r with any value a − k·b, where k is an integer. Why does Euclid's algorithm pick k = q, the quotient, in particular?
Show answer
Any integer k keeps the common divisors unchanged, because a − k·b and a are each integer combinations of the other value and b. Choosing k = q makes the new value the remainder, which satisfies 0 ≤ r < |b|. That gives the smallest non-negative value, so the pair shrinks as fast as possible and the process must reach 0.