Key idea of this section
For a ∈ ℤ and b > 0 there are unique q, r ∈ ℤ with a = qb + r and 0 ≤ r < b. Iterating it computes gcd(a, b) and integers x, y with ax + by = gcd(a, b). A Euclidean domain is an integral domain with such a division; every one is a PID and a UFD.
The division algorithm
Theorem 5.1 (Division with remainder). Let a, b ∈ ℤ with b > 0. There exist unique integers q and r such that a = qb + r and 0 ≤ r < b.
Proof. Existence: the set E = {a − kb : k ∈ ℤ, a − kb ≥ 0} is nonempty (take k = −|a|, which gives a + |a|b ≥ a + |a| ≥ 0), so by well ordering it has a least element r = a − qb ≥ 0. If r ≥ b then r − b = a − (q + 1)b ∈ E is smaller, a contradiction; so 0 ≤ r < b. Uniqueness: if qb + r = q′b + r′ with both remainders in [0, b), then b|q − q′| = |r′ − r| < b, which forces q = q′ and then r = r′. ∎
The quotient is q = ⌊a/b⌋. For negative a the remainder is still nonnegative: −47 = (−8)·6 + 1. Many programming languages instead truncate the quotient toward zero, giving −47 = (−7)·6 − 5; the C99 standard fixed this truncating convention for the operators / and %, which is why −47 % 6 evaluates to −5 in C, Java and JavaScript but to 1 in Python, whose % follows the floor convention.
Greatest common divisors
For integers a, b not both zero, gcd(a, b) is the largest integer dividing both. The key observation is that if a = qb + r then the common divisors of (a, b) and of (b, r) coincide, because any common divisor of two of a, b, r divides the third. Hence gcd(a, b) = gcd(b, r), and repeating the division until the remainder vanishes yields the gcd as the last nonzero remainder. This is Euclid's algorithm (Elements, Book VII, Propositions 1–2, c. 300 BC).
Theorem 5.2 (Bézout's identity). For a, b ∈ ℤ not both zero, there are x, y ∈ ℤ with ax + by = gcd(a, b). Moreover gcd(a, b) is the least positive element of the set aℤ + bℤ, and aℤ + bℤ = gcd(a, b)ℤ.
Proof. Let d be the least positive element of I = {ax + by : x, y ∈ ℤ}, say d = ax + by. Divide a by d: a = qd + r with 0 ≤ r < d. Then r = a − q(ax + by) ∈ I, and minimality forces r = 0, so d divides a; likewise d divides b. Any common divisor of a and b divides every element of I, in particular d. Thus d = gcd(a, b). Since I is closed under subtraction and integer multiples and contains d, it equals dℤ. ∎
Worked Example 5.3 — gcd(1071, 462) with Bézout coefficients. The divisions are:
1071 = 2 · 462 + 147 462 = 3 · 147 + 21 147 = 7 · 21 + 0
so gcd(1071, 462) = 21. Back-substitution gives:
21 = 462 − 3 · 147 = 462 − 3 · (1071 − 2 · 462) = 7 · 462 − 3 · 1071
and indeed 7 · 462 − 3 · 1071 = 3234 − 3213 = 21. In practice one runs the extended Euclidean algorithm forward, maintaining the invariant rᵢ = 1071·xᵢ + 462·yᵢ:
i rᵢ qᵢ xᵢ yᵢ 0 1071 1 0 1 462 2 0 1 2 147 3 1 −2 3 21 7 −3 7 4 0
Each new row is the row two above minus qᵢ times the row immediately above, where qᵢ is the quotient in the division of rᵢ₋₁ by rᵢ. The last nonzero remainder 21 appears with coefficients (−3, 7).
The cost of Euclid's algorithm
Theorem 5.4 (Lamé, 1844). If Euclid's algorithm applied to a > b > 0 takes n division steps, then b ≥ F(n+1) and a ≥ F(n+2), where F(1) = F(2) = 1 and F(k+1) = F(k) + F(k−1). Consequently the number of steps is at most five times the number of decimal digits of b.
Proof. Let r₀ = a, r₁ = b, and let r₂, r₃, … be the successive remainders, with rₙ ≠ 0 and rₙ₊₁ = 0 after n steps. Then rₙ ≥ 1 = F(2), and rₙ₋₁ ≥ 2 = F(3), because rₙ divides rₙ₋₁ and rₙ < rₙ₋₁. Each earlier step has quotient at least 1, so rᵢ₋₁ ≥ rᵢ + rᵢ₊₁, and by downward induction rᵢ ≥ F(n + 2 − i). In particular b = r₁ ≥ F(n + 1) and a = r₀ ≥ F(n + 2). Since F(k) ≥ φk−2 with φ = (1 + √5)/2, we get b ≥ φn−1, so n − 1 ≤ log₁₀ b / log₁₀ φ. If b has d decimal digits then log₁₀ b < d, and log₁₀ φ ≈ 0.20899 > 1/5, so n − 1 < 5d and n ≤ 5d. ∎
Consecutive Fibonacci numbers are the worst case: gcd(13, 8) needs five divisions (13 = 1·8 + 5, 8 = 1·5 + 3, 5 = 1·3 + 2, 3 = 1·2 + 1, 2 = 2·1). Lamé's theorem is often cited as the first result of computational complexity theory: it shows that Euclid's algorithm runs in time polynomial in the number of digits of its input, in fact O(n²) bit operations for n-bit inputs with schoolbook division.
Euclidean domains
Definition 5.5. A Euclidean domain is an integral domain R with a function N : R ∖ {0} → ℕ such that for all a, b ∈ R with b ≠ 0 there are q, r ∈ R with a = qb + r and either r = 0 or N(r) < N(b).
The three classical examples are the following.
- 1.ℤ with N(n) = |n|.
- 2.The polynomial ring k[x] over a field k, with N(f) = deg f. For example, x³ + 2x + 5 = x · (x² + 1) + (x + 5), and deg(x + 5) = 1 < 2.
- 3.The Gaussian integers ℤ[i] = {a + bi : a, b ∈ ℤ}, with N(a + bi) = a² + b².
For ℤ[i], to divide α by β ≠ 0 compute α/β exactly in ℚ(i) and round each coordinate to a nearest integer to get q. The error α/β − q has both coordinates of absolute value at most 1/2, so N(α/β − q) ≤ 1/4 + 1/4 = 1/2, and since N is multiplicative, r = α − qβ satisfies N(r) ≤ N(β)/2 < N(β). For instance, with α = 8 + 9i and β = 2 − i,
α/β = (8 + 9i)(2 + i)/5 = (7 + 26i)/5 = 1.4 + 5.2i, so q = 1 + 5i r = (8 + 9i) − (1 + 5i)(2 − i) = (8 + 9i) − (7 + 9i) = 1, N(r) = 1 < 5 = N(β)
so 8 + 9i and 2 − i are coprime Gaussian integers.
Theorem 5.6. Every Euclidean domain is a principal ideal domain (PID).
Proof. Let I ≠ 0 be an ideal and choose b ∈ I ∖ {0} with N(b) minimal. For a ∈ I write a = qb + r; then r = a − qb ∈ I, and N(r) < N(b) is impossible, so r = 0 and a ∈ (b). Hence I = (b). ∎
Every PID is a unique factorisation domain (UFD), so the chain ED ⇒ PID ⇒ UFD holds; neither implication reverses. The ring ℤ[(1 + √−19)/2] is a PID that is not Euclidean for any function N (Motzkin, 1949), and ℤ[x] is a UFD that is not a PID, since the ideal (2, x) is not principal.
Exercise
- (a)Use the extended Euclidean algorithm to find gcd(240, 46) and integers x, y with 240x + 46y = gcd(240, 46).
- (b)Find q and r for the division of x⁴ + 1 by x² + x + 1 in ℚ[x].
- (c)Divide 11 + 7i by 3 + i in ℤ[i], giving q and r with r = 0 or N(r) < N(3 + i).
Show solution
Answers: (a) 240 = 5·46 + 10, 46 = 4·10 + 6, 10 = 1·6 + 4, 6 = 1·4 + 2, 4 = 2·2, so the gcd is 2. Back-substituting: 2 = 6 − 4 = 6 − (10 − 6) = 2·6 − 10 = 2(46 − 4·10) − 10 = 2·46 − 9·10 = 2·46 − 9(240 − 5·46) = 47·46 − 9·240; check 2162 − 2160 = 2. So x = −9, y = 47. (b) x⁴ + 1 = (x² − x)(x² + x + 1) + (x + 1); check: (x² − x)(x² + x + 1) = x⁴ − x, and adding x + 1 gives x⁴ + 1. So q = x² − x and r = x + 1. (c) (11 + 7i)/(3 + i) = (11 + 7i)(3 − i)/10 = (40 + 10i)/10 = 4 + i exactly, so q = 4 + i and r = 0: indeed (4 + i)(3 + i) = 12 + 4i + 3i − 1 = 11 + 7i.