Sub topic 1
Divisors, Common Divisors, and the GCD
Euclid's algorithm computes one number: the greatest common divisor of two integers. To see why the algorithm works, you first need exact definitions of what it computes. This chapter gives those definitions, a slow but obviously correct way to compute the GCD, and one property of divisibility that later chapters depend on.
Prerequisites
This chapter assumes you can:
- Do basic integer arithmetic, including with negative numbers.
- Use the remainder operator
%in Python. - Read a short Python function that uses sets and comprehensions.
Proofs by induction and recursion are not needed yet. They appear in later chapters.
Divisibility
An integer d divides an integer n if some integer k satisfies n = d * k. We write this as d | n and say that d is a divisor of n, or that n is a multiple of d.
Some consequences follow directly from the definition:
3 | 12because12 = 3 * 4.-3 | 12because12 = (-3) * (-4). Divisors can be negative, but by convention we usually list only the positive ones.5 | 0because0 = 5 * 0. Every integer divides 0.0 | nonly whenn = 0.
In code, for d != 0, d | n is the same test as n % d == 0.
Common Divisors and the Greatest One
A common divisor of a and b is an integer that divides both. The greatest common divisor, gcd(a, b), is the largest of them.
Take 48 and 18:
| Number | Positive divisors |
|---|---|
| 48 | 1, 2, 3, 4, 6, 8, 12, 16, 24, 48 |
| 18 | 1, 2, 3, 6, 9, 18 |
| Common | 1, 2, 3, 6 |
The largest common divisor is 6, so gcd(48, 18) = 6.
Special cases
- One input is zero. Every integer divides 0, so the common divisors of
aand0are exactly the divisors ofa. That makesgcd(a, 0) = |a|. The answer is not 0. - Both inputs are zero. Every integer divides both, so no largest one exists. By convention
gcd(0, 0) = 0, and Python'smath.gcd(0, 0)returns 0. - Signs.
aand-ahave the same divisors, sogcd(a, b) = gcd(|a|, |b|). The GCD is never negative.
Two integers whose GCD is 1 are called coprime, or relatively prime. For example, 8 and 15 are coprime even though neither one is prime.
A Brute-Force GCD
The definition gives an algorithm directly: list the divisors of each number, intersect the two lists, and take the maximum.
def divisors(n):
n = abs(n)
return {d for d in range(1, n + 1) if n % d == 0}
def gcd_brute(a, b):
a, b = abs(a), abs(b)
if a == 0:
return b # covers gcd(0, 0) = 0 and gcd(0, b) = |b|
if b == 0:
return a
return max(divisors(a) & divisors(b))
print(sorted(divisors(48) & divisors(18))) # [1, 2, 3, 6]
print(gcd_brute(48, 18)) # 6
print(gcd_brute(-12, 0)) # 12This code is correct, but it tries every candidate up to |n|, so its work grows linearly with the size of the input. For a 20-digit number that means about 10^20 trial divisions. Euclid's algorithm gets the same answer in a few dozen steps. Use gcd_brute as a reference to test faster versions against.
The Property Everything Else Depends On
If d | a and d | b, then d divides every integer combination a*x + b*y.
Proof. Write a = d*k and b = d*m. Then a*x + b*y = d*(k*x + m*y), and k*x + m*y is an integer.
Two special cases come up often: d | (a - b), and d | (a - q*b) for any integer q. The second case is why Euclid's algorithm is allowed to replace a with a remainder. A later chapter turns this into a full proof.
One more fact will be proved later with Bézout's identity: every common divisor of a and b divides gcd(a, b). The GCD is the largest common divisor, and it is also a multiple of every other common divisor.
Common Errors
- Saying that
gcd(a, 0)is 0 or undefined. It is|a|. - Returning a negative GCD for negative inputs.
- Reading
d | nas division. - Assuming that coprime numbers must be prime.
Exercise
List the common divisors of 84 and 120 and give gcd(84, 120). Then give gcd(-84, 0) and gcd(0, 0).
Show answer
The divisors of 84 are 1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42 and 84. The divisors of 120 are 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60 and 120. The common divisors are 1, 2, 3, 4, 6 and 12, so gcd(84, 120) = 12. gcd(-84, 0) = 84, and gcd(0, 0) = 0 by convention.
Exercise
Suppose d divides both 91 and 35. Without factoring, explain why d must divide 21.
Show answer
d divides every integer combination of 91 and 35. 91 - 2*35 = 21 is such a combination, so d divides 21.