taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Volume P1 · Proof, induction and invariants

Strong induction and well ordering

Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.

Read first
Contents
  1. Strong induction
  2. Well ordering
  3. The greatest common divisor and Euclid's algorithm

Strong induction

Statement

Theorem P1.5.1 (the principle of strong induction). Let P(n)P(n) be a statement about the natural number nn, and let aa be a natural number. If for every k≥ak \ge a the following is true,

if P(j) holds for every j with a≤j<k, then P(k) holds,\text{if } P(j) \text{ holds for every } j \text{ with } a \le j < k, \text{ then } P(k) \text{ holds,}

then P(n)P(n) holds for every n≥an \ge a.

Lean: Nat.strong_induction_on and the tactic induction n using Nat.strong_induction_on with | _ n ih => ... (Mathlib); Nat.strongRecOn is the same principle for definitions.

The hypothesis for k=ak = a has no jj with a≤j<aa \le j < a, so it says that P(a)P(a) is true outright. A proof by strong induction may therefore have no separate base case, but it often has one, since the argument for the least values is often a special one. The proof has two parts: let k≥ak \ge a and assume P(j)P(j) for all a≤j<ka \le j < k, and prove P(k)P(k). The assumption is the strong induction hypothesis.

Theorem P1.5.2 (strong induction from weak induction). The principle of strong induction follows from the principle of induction of Chapter 4.

Proof. Assume the step of the principle of strong induction. Let Q(n)Q(n) be the statement "P(j)P(j) for all jj with a≤j≤na \le j \le n", for n≥a−1n \ge a - 1 (for n=a−1n = a - 1 the statement is empty and true). Base: Q(a−1)Q(a - 1) is true. Step: assume Q(k)Q(k) for some k≥a−1k \ge a - 1, that is, P(j)P(j) for all a≤j≤ka \le j \le k. This is the hypothesis of the principle for the number k+1k + 1, so P(k+1)P(k + 1) holds, and with Q(k)Q(k) this is Q(k+1)Q(k + 1). By induction Q(n)Q(n) holds for all n≥a−1n \ge a - 1, so P(n)P(n) holds for all n≥an \ge a. □\square

The proof shows that the two principles have the same strength: weak induction is the special case of strong induction that uses only P(k−1)P(k - 1).

Example P1.5.3 (postage). Claim: every amount of 1212 or more can be paid with stamps of 44 and 55. Strong induction with a=12a = 12. The amounts 12=3⋅412 = 3 \cdot 4, 13=2⋅4+513 = 2 \cdot 4 + 5, 14=4+2⋅514 = 4 + 2 \cdot 5 and 15=3⋅515 = 3 \cdot 5 are cases of a base. For k≥16k \ge 16 the amount k−4≥12k - 4 \ge 12 can be paid by the hypothesis, and one more stamp of 44 pays kk. The amounts 1,2,3,6,7,111, 2, 3, 6, 7, 11 cannot be paid, which a computer search confirms, so the claim is best possible: 1111 is not payable.

Example P1.5.4 (the binary representation). Claim: every integer n≥1n \ge 1 is a sum of distinct powers of 22. Let k≥1k \ge 1 and assume the claim for every jj with 1≤j<k1 \le j < k. If k=1=20k = 1 = 2^0, it is done. Otherwise let 2m2^m be the largest power of 22 with 2m≤k2^m \le k. If k=2mk = 2^m, it is done. If not, r=k−2mr = k - 2^m satisfies 1≤r<2m1 \le r < 2^m (since k<2m+1k < 2^{m+1}), and r<kr < k. By the hypothesis, rr is a sum of distinct powers of 22, all below 2m2^m, since their sum is below 2m2^m. Adding 2m2^m gives kk. For k=100=64+32+4k = 100 = 64 + 32 + 4.

Theorem P1.5.5 (a prime divisor). Every integer n≥2n \ge 2 has a prime divisor.

Proof. A prime is an integer p≥2p \ge 2 whose only positive divisors are 11 and pp. Let k≥2k \ge 2 and assume the claim for every jj with 2≤j<k2 \le j < k. If kk is prime, then kk divides kk. If not, then k=abk = ab with 1<a<k1 < a < k and 1<b<k1 < b < k. By the hypothesis, aa has a prime divisor pp, and p∣a∣kp \mid a \mid k. □\square

Theorem P1.5.6 (existence of a prime factorization). Every integer n≥2n \ge 2 is a product of primes, with a prime counted as a product of one factor.

Lean: Nat.exists_prime_and_dvd gives the prime divisor (Mathlib); the list of prime factors is Nat.primeFactorsList in recent versions and Nat.factors in older ones.

Proof. Let k≥2k \ge 2 and assume the claim for all jj with 2≤j<k2 \le j < k. If kk is prime, the product has one factor. If not, k=abk = ab with 1<a,b<k1 < a, b < k, and by the hypothesis aa and bb are products of primes. The product of the two lists is a list for kk. □\square

Example P1.5.7. 360=2⋅2⋅2⋅3⋅3⋅5360 = 2 \cdot 2 \cdot 2 \cdot 3 \cdot 3 \cdot 5 and 2023=7⋅17⋅172023 = 7 \cdot 17 \cdot 17. A proof by weak induction would need P(k)P(k) to give the factorization of the factors aa and bb of kk, which are not k−1k - 1.

Example P1.5.8 (a recurrence that refers to half). Let T(1)=1T(1) = 1 and T(n)=T(⌊n/2⌋)+1T(n) = T(\lfloor n/2 \rfloor) + 1 for n≥2n \ge 2. Claim: T(n)=⌊log⁡2n⌋+1T(n) = \lfloor \log_2 n \rfloor + 1, which is the number of digits of nn in base 22. Let k≥2k \ge 2 and assume the claim for 1≤j<k1 \le j < k. Since ⌊k/2⌋<k\lfloor k/2 \rfloor < k, T(k)=⌊log⁡2⌊k/2⌋⌋+2T(k) = \lfloor \log_2 \lfloor k/2 \rfloor \rfloor + 2. If 2m≤k<2m+12^m \le k < 2^{m+1}, then 2m−1≤⌊k/2⌋<2m2^{m-1} \le \lfloor k/2 \rfloor < 2^m, so ⌊log⁡2⌊k/2⌋⌋=m−1\lfloor \log_2 \lfloor k/2 \rfloor \rfloor = m - 1 and T(k)=m+1=⌊log⁡2k⌋+1T(k) = m + 1 = \lfloor \log_2 k \rfloor + 1. For n=1000n = 1000, T=10T = 10.

Proof

The principle of strong induction was proved above. A proof by it follows a plan.

  1. Say that the induction is strong, with the statement P(n)P(n) and the first value aa.
  2. Let k≥ak \ge a be arbitrary and assume P(j)P(j) for all a≤j<ka \le j < k.
  3. Prove P(k)P(k), with cases if needed: a case for the small kk that has no usable jj, and a case for the larger kk that reduces to smaller values.
  4. Check that each smaller value used is at least aa and less than kk.

The usual error is at step 4. A proof that uses P(k−2)P(k - 2) for all kk is valid only for k−2≥ak - 2 \ge a, and the cases k=ak = a and k=a+1k = a + 1 need their own proof. The example of postage has four base cases for this reason: the step uses k−4k - 4, so the first four values must be done by hand.

Examples

Example P1.5.9 (every n≥12n \ge 12 with stamps of 33 and 77). Strong induction with the base cases 12=4⋅312 = 4 \cdot 3, 13=2⋅3+713 = 2 \cdot 3 + 7 and 14=2⋅714 = 2 \cdot 7, and the step k→k+3k \to k + 3 for k≥15k \ge 15 (use k−3≥12k - 3 \ge 12). The amounts that cannot be paid are 1,2,4,5,8,111, 2, 4, 5, 8, 11.

Example P1.5.10 (Zeckendorf's representation). Claim: every integer n≥1n \ge 1 is a sum of Fibonacci numbers with distinct indices at least 22. Let k≥1k \ge 1 and assume the claim for 1≤j<k1 \le j < k. Let FmF_m be the largest Fibonacci number with m≥2m \ge 2 and Fm≤kF_m \le k. If k=Fmk = F_m, done. Otherwise r=k−Fm≥1r = k - F_m \ge 1, and k<Fm+1k < F_{m+1} gives r<Fm+1−Fm=Fm−1r < F_{m+1} - F_m = F_{m-1}, so the numbers of the representation of rr have indices at most m−2m - 2, and FmF_m can be added with a different index. For k=100k = 100, the largest is F11=89F_{11} = 89, and 100=89+8+3100 = 89 + 8 + 3.

Example P1.5.11 (a false proof). Claim: every integer n≥2n \ge 2 is even. Strong induction: let k≥2k \ge 2, and assume the claim for 2≤j<k2 \le j < k. Write k=(k−2)+2k = (k - 2) + 2, a sum of two even numbers. The step is false for k=3k = 3, since k−2=1<2k - 2 = 1 < 2, and for k=2k = 2 there is no base case. The claim is false for n=3n = 3.

Exercises

  1. [10] Show that every amount of 1212 or more can be paid with stamps of 44 and 55, and list the amounts that cannot.
  2. [12] Show that every amount of 1212 or more can be paid with stamps of 33 and 77, and list the amounts that cannot.
  3. [14] Prove that every integer n≥1n \ge 1 is a sum of distinct powers of 22, and write 7575 in this form.
  4. [14] Prove that every integer n≥2n \ge 2 has a prime divisor, and find the least prime divisor of 20232023 and of 3003130031.
  5. [15] Prove that every integer n≥2n \ge 2 is a product of primes, and write 10011001 and 10241024 as products of primes.
  6. [16] Let T(1)=1T(1) = 1 and T(n)=T(⌊n/2⌋)+1T(n) = T(\lfloor n/2 \rfloor) + 1. Prove that T(n)=⌊log⁡2n⌋+1T(n) = \lfloor \log_2 n \rfloor + 1 for n≥1n \ge 1, and find T(1000)T(1000).
  7. [M20] Prove that every integer n≥1n \ge 1 is a sum of Fibonacci numbers with distinct indices at least 22, and write 100100 in this form.
  8. [M20] Prove that the number of prime factors of n≥2n \ge 2, counted with multiplicity, is at most log⁡2n\log_2 n.
  9. [M22] Show that strong induction follows from weak induction by the statement Q(n)Q(n) of the text, and show that weak induction follows from strong induction.
  10. [M25] Find the error: "Claim: every integer n≥2n \ge 2 is even. Proof: for k≥2k \ge 2, k=(k−2)+2k = (k - 2) + 2 is a sum of two even numbers by the hypothesis." Say which cases of the step fail.

Lean

The statement ∀ n, P n is proved with induction n using Nat.strong_induction_on with | _ n ih => ..., where ih : ∀ m < n, P m. The existence of a prime divisor is Nat.exists_prime_and_dvd : n ≠ 1 → ∃ p, Nat.Prime p ∧ p ∣ n. The least prime divisor is Nat.minFac, and Nat.minFac_prime : n ≠ 1 → (Nat.minFac n).Prime states that it is prime. A check of 2023=7⋅1722023 = 7 \cdot 17^2 is by norm_num.

Notes and history

Strong induction is also called complete induction or course-of-values induction. The binary representation is in the work of Leibniz of 1703 (Explication de l'arithmétique binaire). The representation of Zeckendorf is from 1972, and Lekkerkerker found it in 1952. The existence of a prime factorization is in Book VII of Euclid, and its uniqueness is the subject of Section 3. TAOCP Section 1.2.1 allows the proof of P(n)P(n) to use all of P(1),…,P(n−1)P(1), \dots, P(n - 1), which is the strong form.

Well ordering

Statement

Definition P1.5.12 (least element). Let SS be a set of integers. An element m∈Sm \in S is a least element of SS if m≤sm \le s for every s∈Ss \in S. A least element is unique: if mm and m′m' are both least, then m≤m′m \le m' and m′≤mm' \le m.

Axiom or theorem (the well-ordering principle). Every nonempty set of natural numbers has a least element.

Lean: Nat.find with Nat.find_spec and Nat.find_min; WellFounded.min and Nat.lt_wfRel state the same fact in the language of well-founded relations (Mathlib).

The principle fails for other sets. The set of integers has no least element, and the set of positive rationals has none either: for each x>0x > 0 the number x/2x/2 is positive and smaller.

Theorem P1.5.13 (the three principles are equivalent). Weak induction, strong induction and well ordering imply each other.

Proof. Weak induction implies strong induction (Section 1). Strong induction implies well ordering: let SS be a set of natural numbers with no least element, and let P(n)P(n) be "n∉Sn \notin S". Let kk be a natural number and assume P(j)P(j) for all j<kj < k. If k∈Sk \in S, then kk is a least element, since no smaller number is in SS, which contradicts the choice of SS. So k∉Sk \notin S. By strong induction SS has no element, that is, SS is empty. So a nonempty set has a least element. Well ordering implies weak induction: this is the proof of Chapter 4, Section 1. □\square

Definition P1.5.14 (the method of the least counterexample). To prove P(n)P(n) for all n≥an \ge a by well ordering, suppose it is false for some nn. The set C={n≥a:P(n) is false}C = \{n \ge a : P(n) \text{ is false}\} is nonempty, so it has a least element mm. Show that mm cannot exist: either P(m)P(m) is true after all, or a smaller element of CC exists.

Example P1.5.15. Claim: n<2nn < 2^n for n≥0n \ge 0. Suppose it is false, and let mm be the least nn with n≥2nn \ge 2^n. Since 0<10 < 1, m≥1m \ge 1. The number m−1m - 1 is smaller than mm, so m−1<2m−1m - 1 < 2^{m-1}, which gives m≤2m−1m \le 2^{m-1}. Then 2m=2⋅2m−1≥2m≥m+1>m2^m = 2 \cdot 2^{m-1} \ge 2m \ge m + 1 > m, which contradicts m≥2mm \ge 2^m.

Theorem P1.5.16 (the division theorem). Let aa and bb be integers with b>0b > 0. There are unique integers qq and rr with a=qb+ra = qb + r and 0≤r<b0 \le r < b.

Lean: Int.ediv_add_emod : b * (a / b) + a % b = a, with Int.emod_nonneg and Int.emod_lt_of_pos for the bounds on the remainder (Lean core).

Proof of existence. The set S={a−qb:q∈ℤ,a−qb≥0}S = \{a - qb : q \in \mathbb Z, a - qb \ge 0\} is nonempty: for q=−∣a∣q = -|a| the number a+∣a∣b≥a+∣a∣≥0a + |a| b \ge a + |a| \ge 0. By well ordering SS has a least element r=a−qb≥0r = a - qb \ge 0. If r≥br \ge b, then r−b=a−(q+1)br - b = a - (q + 1)b is in SS and smaller than rr, a contradiction. So 0≤r<b0 \le r < b.

Proof of uniqueness. Let a=qb+r=q′b+r′a = qb + r = q'b + r' with 0≤r,r′<b0 \le r, r' < b. Then (q−q′)b=r′−r(q - q')b = r' - r, and ∣r′−r∣<b|r' - r| < b. The multiple (q−q′)b(q - q')b of bb with absolute value less than bb is 00. So q=q′q = q' and r=r′r = r'. □\square

The number qq is the quotient and rr is the remainder. For a=−17a = -17 and b=5b = 5: −17=(−4)⋅5+3-17 = (-4) \cdot 5 + 3, so q=−4q = -4 and r=3r = 3, and the remainder is not negative. The numbers are ⌊a/b⌋\lfloor a/b \rfloor and a mod ba \bmod b in TAOCP (Section 1.2.4).

Proof

The division theorem is the first use of well ordering that makes a number. The set SS of the proof is the set of all values a−qba - qb that are not negative. The remainder is its least element. The same plan serves in many proofs: define the set of numbers that have a property, show that it is not empty, take the least element, and show that a smaller one exists if the least element lacks a second property.

Theorem P1.5.17 (Euclid's algorithm ends). Let mm and nn be natural numbers with n>0n > 0. Consider the repeated step: let rr be the remainder of mm divided by nn; if r=0r = 0 stop; otherwise replace (m,n)(m, n) by (n,r)(n, r). The steps cannot go on forever.

Proof. Suppose they go on forever, with the values n=n0>n1>n2>⋯n = n_0 > n_1 > n_2 > \cdots of the second number before each step. Each ni+1n_{i+1} is a remainder of a division by nin_i, so 0<ni+1<ni0 < n_{i+1} < n_i (it is not 00, since the algorithm did not stop). The set {n0,n1,… }\{n_0, n_1, \dots\} of natural numbers is nonempty, so it has a least element ntn_t. But nt+1<ntn_{t+1} < n_t is in the set, a contradiction. □\square

The proof gives also a bound: the number of steps is at most nn, since each step lowers the second number by at least 11 and it stays positive. The bound is poor, and Section 3 gives a good one.

Examples

Example P1.5.18 (2\sqrt 2 by least denominator). Suppose 2\sqrt 2 is rational. Then the set SS of positive integers qq such that q2q\sqrt 2 is an integer is nonempty. Let qq be its least element and p=q2p = q\sqrt 2. Since 1<2<21 < \sqrt 2 < 2, q<p<2qq < p < 2q. Let q′=p−qq' = p - q. Then 0<q′<q0 < q' < q, and q′2=p2−q2=2q−pq'\sqrt 2 = p\sqrt 2 - q\sqrt 2 = 2q - p is an integer. So q′∈Sq' \in S is smaller than qq, a contradiction. This is a proof without the notion of lowest terms.

Example P1.5.19 (a greatest element). Claim: a nonempty set SS of natural numbers with an upper bound BB has a greatest element. The set T={B−s:s∈S}T = \{B - s : s \in S\} is a nonempty set of natural numbers, so it has a least element B−s∗B - s^*. Then s∗s^* is the greatest element of SS, since B−s∗≤B−sB - s^* \le B - s gives s≤s∗s \le s^* for every s∈Ss \in S.

Example P1.5.20 (the number of digits). For each integer n≥1n \ge 1 the set of mm with 10m>n10^m > n is nonempty, so it has a least element dd, and 10d−1≤n<10d10^{d-1} \le n < 10^d. This dd is the number of digits of nn in base 1010. For n=2023n = 2023 it is 44, and for n=106n = 10^6 it is 77.

Exercises

  1. [10] Find a nonempty set of integers with no least element, and a nonempty set of rational numbers with no least element, and a set of positive rationals bounded below with no least element.
  2. [12] Find the quotient and remainder of the division of aa by bb for (a,b)=(100,7)(a, b) = (100, 7), (−100,7)(-100, 7), (0,5)(0, 5) and (3,5)(3, 5).
  3. [14] Prove by the method of the least counterexample that n<2nn < 2^n for n≥0n \ge 0.
  4. [15] Prove the uniqueness of the quotient and the remainder in the division theorem, and show by an example that uniqueness fails if the condition 0≤r<b0 \le r < b is replaced by ∣r∣<b|r| < b.
  5. [15] Prove that strong induction implies the well-ordering principle.
  6. [M18] Prove that there is no integer nn with 0<n<10 < n < 1, by well ordering.
  7. [M20] Prove by well ordering that 3\sqrt 3 is irrational, with the set of positive integers qq such that q3q\sqrt 3 is an integer.
  8. [M20] Prove that every nonempty set of natural numbers that has an upper bound has a greatest element.
  9. [M22] Let the steps of Euclid's algorithm on (m,n)(m, n) with m≥n>0m \ge n > 0 be run. Prove that the remainders r1,r2,…r_1, r_2, \dots strictly decrease, and that the second number is halved at least every two steps: ri+2<ri/2r_{i+2} < r_i / 2.
  10. [M25] Let SS be a set of natural numbers with 0∈S0 \in S, and with k+1∈Sk + 1 \in S whenever all of 0,…,k∈S0, \dots, k \in S. Prove by well ordering that SS is the set of all natural numbers.

Lean

Nat.find gives the least element of a set given by a decidable property with a proof of nonemptiness: Nat.find h with h : ∃ n, P n, Nat.find_spec h : P (Nat.find h) and Nat.find_min h : m < Nat.find h → ¬ P m. This is the method of the least counterexample as a program. The division theorem is in the core as Int.ediv_add_emod, and the statements Nat.mod_lt and Nat.div_add_mod do the same for the natural numbers.

Notes and history

The well-ordering principle was an axiom in early treatments of the integers and a theorem in the treatment of Peano; the proof of the equivalence above is in many books. The method of the least counterexample is infinite descent in the form of Fermat, who used it for number theory around 1640. Euclid's Elements, Book VII, gives the algorithm for the greatest common divisor by repeated subtraction. TAOCP Section 1.1 argues that Algorithm E ends because the second number decreases at each step, which is the theorem above.

The greatest common divisor and Euclid's algorithm

Statement

Definition P1.5.21 (common divisors). For an integer m≠0m \ne 0 let D(m)D(m) be the set of positive divisors of mm. A common divisor of mm and nn is an element of D(m)∩D(n)D(m) \cap D(n). If mm and nn are not both 00, the set is finite, and its greatest element is the greatest common divisor gcd⁡(m,n)\gcd(m, n). By convention gcd⁡(0,0)=0\gcd(0, 0) = 0, and every positive integer divides 00.

Lean: Nat.gcd and Int.gcd, with Nat.gcd_dvd_left, Nat.gcd_dvd_right and Nat.dvd_gcd (Lean core and Mathlib).

Example P1.5.22. D(12)={1,2,3,4,6,12}D(12) = \{1, 2, 3, 4, 6, 12\} and D(18)={1,2,3,6,9,18}D(18) = \{1, 2, 3, 6, 9, 18\}. The common divisors are 1,2,3,61, 2, 3, 6, so gcd⁡(12,18)=6\gcd(12, 18) = 6. Also gcd⁡(m,0)=∣m∣\gcd(m, 0) = |m| for m≠0m \ne 0, since each divisor of mm divides 00.

Lemma P1.5.23 (the key lemma of Euclid). If m=qn+rm = qn + r for integers m,n,q,rm, n, q, r, then mm and nn have the same common divisors as nn and rr. So gcd⁡(m,n)=gcd⁡(n,r)\gcd(m, n) = \gcd(n, r), and D(m)∩D(n)=D(n)∩D(r)D(m) \cap D(n) = D(n) \cap D(r) when these are defined.

Proof. Let d∣md \mid m and d∣nd \mid n. Then d∣m−qn=rd \mid m - qn = r (Chapter 3). Conversely, let d∣nd \mid n and d∣rd \mid r. Then d∣qn+r=md \mid qn + r = m. So a number divides both mm and nn iff it divides both nn and rr. □\square

Lean: Nat.gcd_rec : Nat.gcd m n = Nat.gcd (n % m) m (Lean core), which is the lemma in the form that the algorithm uses.

Theorem P1.5.24 (Algorithm 1.1E). Let mm and nn be positive integers. Euclid's algorithm is:

  • E1. Divide mm by nn and let rr be the remainder.
  • E2. If r=0r = 0, the algorithm ends and nn is the answer.
  • E3. Set m←nm \gets n and n←rn \gets r, and go back to E1.

It ends, and the answer is gcd⁡(m,n)\gcd(m, n).

Lean: the function is Nat.gcd, which is defined by this recursion, and Nat.gcd_rec states it.

Proof. The algorithm ends by the theorem of Section 2. For the answer, prove by strong induction on n≥1n \ge 1 the statement P(n)P(n): for every positive mm, the algorithm started on (m,n)(m, n) ends with the answer gcd⁡(m,n)\gcd(m, n). Let n≥1n \ge 1 and assume P(j)P(j) for 1≤j<n1 \le j < n. Let m≥1m \ge 1 and let rr be the remainder of mm by nn, so m=qn+rm = qn + r and 0≤r<n0 \le r < n. If r=0r = 0, the algorithm answers nn, and n∣mn \mid m, so the common divisors of mm and nn are the divisors of nn, with greatest element nn. If r>0r > 0, the algorithm continues on (n,r)(n, r) with 1≤r<n1 \le r < n, and by the hypothesis it ends with the answer gcd⁡(n,r)\gcd(n, r). By the key lemma gcd⁡(n,r)=gcd⁡(m,n)\gcd(n, r) = \gcd(m, n). □\square

Example P1.5.25. For m=544m = 544 and n=119n = 119 the steps are 544=4⋅119+68544 = 4 \cdot 119 + 68, 119=1⋅68+51119 = 1 \cdot 68 + 51, 68=1⋅51+1768 = 1 \cdot 51 + 17, 51=3⋅17+051 = 3 \cdot 17 + 0. The answer is 1717, in four divisions. For (2024,1024)(2024, 1024) the answer is 88 in five divisions.

Theorem P1.5.26 (the identity of Bezout). For integers mm and nn, not both 00, there are integers xx and yy with mx+ny=gcd⁡(m,n)mx + ny = \gcd(m, n).

Lean: Nat.gcd_eq_gcd_ab : (Nat.gcd x y : ℤ) = x * Nat.gcdA x y + y * Nat.gcdB x y (Mathlib).

Proof. The set LL of positive integers of the form mx+nymx + ny is nonempty, since it holds m2+n2>0m^2 + n^2 > 0. By well ordering let g=mx0+ny0g = mx_0 + ny_0 be its least element. To show that gg divides mm, use the division theorem: m=qg+rm = qg + r with 0≤r<g0 \le r < g. Then r=m−qg=m(1−qx0)+n(−qy0)r = m - qg = m(1 - qx_0) + n(-qy_0) has the form mx+nymx + ny, and if r>0r > 0 it would be an element of LL smaller than gg. So r=0r = 0 and g∣mg \mid m. The same proof gives g∣ng \mid n. Thus gg is a common divisor, and g≤gcd⁡(m,n)g \le \gcd(m, n). Conversely, gcd⁡(m,n)\gcd(m, n) divides mx0+ny0=gmx_0 + ny_0 = g, so gcd⁡(m,n)≤g\gcd(m, n) \le g. Hence g=gcd⁡(m,n)g = \gcd(m, n). □\square

Corollary P1.5.27. A common divisor of mm and nn divides gcd⁡(m,n)\gcd(m, n). The equation mx+ny=cmx + ny = c has an integer solution iff gcd⁡(m,n)∣c\gcd(m, n) \mid c. Two integers are coprime, with gcd⁡=1\gcd = 1, iff mx+ny=1mx + ny = 1 has a solution.

Proof. A common divisor dd divides mx0+ny0=gcd⁡(m,n)mx_0 + ny_0 = \gcd(m, n). If c=kgcd⁡(m,n)c = k \gcd(m, n), take the solution kx0kx_0, ky0ky_0. If mx+ny=cmx + ny = c, then gcd⁡(m,n)\gcd(m, n) divides cc since it divides mm and nn. □\square

Lemma P1.5.28 (Euclid's lemma). If gcd⁡(a,b)=1\gcd(a, b) = 1 and a∣bca \mid bc, then a∣ca \mid c. If pp is a prime and p∣bcp \mid bc, then p∣bp \mid b or p∣cp \mid c.

Proof. Let ax+by=1ax + by = 1. Then c=acx+bcyc = acx + bcy. Since a∣acxa \mid acx and a∣bca \mid bc, it follows that a∣ca \mid c. For the prime, if p∤bp \nmid b, then gcd⁡(p,b)=1\gcd(p, b) = 1 since the only positive divisors of pp are 11 and pp, and the first part applies. □\square

This lemma gives the uniqueness of a prime factorization, the second half of the theorem of Euclid: two factorizations of a number into primes agree up to the order of the factors. The proof is by strong induction using Euclid's lemma, and it is Exercise 9. Chapter 3 has shown that the lemma is false for a composite aa: 6∣2⋅36 \mid 2 \cdot 3 but 6∤26 \nmid 2 and 6∤36 \nmid 3.

Proof

The extended algorithm finds the numbers xx and yy of Bezout together with the answer. Its proof is by the method of Chapter 4, Section 3: induction on the passes of a loop. Run Euclid's algorithm on (M,N)(M, N), and keep four more integers a,b,c,da, b, c, d with the invariant

m=aM+bN,n=cM+dN.m = aM + bN, \qquad n = cM + dN.

At the start m=Mm = M and n=Nn = N, so take (a,b,c,d)=(1,0,0,1)(a, b, c, d) = (1, 0, 0, 1). In a pass let qq be the quotient of mm by nn. The new pair is (n,m−qn)(n, m - qn), and by the invariant m−qn=(a−qc)M+(b−qd)Nm - qn = (a - qc)M + (b - qd)N. So the new coefficients are (c,d,a−qc,b−qd)(c, d, a - qc, b - qd) and the invariant holds again. When n=0n = 0, the answer is m=aM+bNm = aM + bN.

Example P1.5.29. For (M,N)=(544,119)(M, N) = (544, 119) the quotients are 4,1,1,34, 1, 1, 3 and the rows (m,n;a,b;c,d)(m, n; a, b; c, d) are

(544,119;1,0;0,1), (119,68;0,1;1,−4), (68,51;1,−4;−1,5), (51,17;−1,5;2,−9), (17,0;2,−9;−7,32).(544, 119; 1, 0; 0, 1),\ (119, 68; 0, 1; 1, -4),\ (68, 51; 1, -4; -1, 5),\ (51, 17; -1, 5; 2, -9),\ (17, 0; 2, -9; -7, 32).

The answer is 17=2⋅544−9⋅11917 = 2 \cdot 544 - 9 \cdot 119. Check: 1088−1071=171088 - 1071 = 17.

Example P1.5.30 (the inverse modulo a number). If gcd⁡(a,p)=1\gcd(a, p) = 1, the numbers x,yx, y of Bezout satisfy ax+py=1ax + py = 1, so ax≡1(modp)ax \equiv 1 \pmod p and xx is an inverse of aa modulo pp. For a=17a = 17 and p=31p = 31 the extended algorithm gives 17⋅11+31⋅(−6)=187−186=117 \cdot 11 + 31 \cdot (-6) = 187 - 186 = 1, so 1111 is the inverse of 1717 modulo 3131.

Examples

Example P1.5.31 (the worst case). For consecutive Fibonacci numbers, Euclid's algorithm is slowest: gcd⁡(Fk+1,Fk)\gcd(F_{k+1}, F_k) takes k−1k - 1 divisions for k≥2k \ge 2, since Fk+1=1⋅Fk+Fk−1F_{k+1} = 1 \cdot F_k + F_{k-1} with Fk−1<FkF_{k-1} < F_k gives the quotient 11 each time. For gcd⁡(89,55)\gcd(89, 55) the sequence is 89,55,34,21,13,8,5,3,2,189, 55, 34, 21, 13, 8, 5, 3, 2, 1, which is 99 divisions (k=10k = 10). The theorem of Lame of 1844 says that if the algorithm on m>n≥1m > n \ge 1 takes kk divisions, then n≥Fk+1n \ge F_{k+1} and m≥Fk+2m \ge F_{k+2}, so kk is about log⁡φn\log_\varphi n.

Example P1.5.32 (coprime pairs). The number of pairs (m,n)(m, n) with 1≤m,n≤1001 \le m, n \le 100 and gcd⁡(m,n)=1\gcd(m, n) = 1 is 60876087 by a computer count, a little over 6060 percent of the 1000010000 pairs.

Example P1.5.33 (a divisor that ends the algorithm at once). For (1001,91)(1001, 91) the first division gives 1001=11⋅91+01001 = 11 \cdot 91 + 0, so the answer is 9191 after one division. The numbers of Bezout are x=0x = 0 and y=1y = 1.

Exercises

  1. [10] Run Euclid's algorithm on (544,119)(544, 119) and on (2024,1024)(2024, 1024), and count the divisions.
  2. [12] Find gcd⁡(240,46)\gcd(240, 46) and numbers xx and yy with 240x+46y=gcd⁡(240,46)240x + 46y = \gcd(240, 46).
  3. [14] Prove the key lemma: if m=qn+rm = qn + r then gcd⁡(m,n)=gcd⁡(n,r)\gcd(m, n) = \gcd(n, r).
  4. [14] Find the inverse of 1717 modulo 3131 and the inverse of 55 modulo 1212.
  5. [15] Prove that gcd⁡(ka,kb)=kgcd⁡(a,b)\gcd(ka, kb) = k \gcd(a, b) for k>0k > 0.
  6. [M18] Prove that if gcd⁡(a,b)=1\gcd(a, b) = 1 and a∣bca \mid bc then a∣ca \mid c.
  7. [M20] Prove that gcd⁡(m,n)=1\gcd(m, n) = 1 and gcd⁡(m,n′)=1\gcd(m, n') = 1 imply gcd⁡(m,nn′)=1\gcd(m, nn') = 1.
  8. [M22] Prove that gcd⁡(Fk+1,Fk)=1\gcd(F_{k+1}, F_k) = 1, and that the algorithm on them takes k−1k - 1 divisions for k≥2k \ge 2.
  9. [M25] Prove the uniqueness of the prime factorization by strong induction, using Euclid's lemma.
  10. [M25] Prove that mx+ny=cmx + ny = c has an integer solution iff gcd⁡(m,n)∣c\gcd(m, n) \mid c, and find all solutions of 15x+21y=915x + 21y = 9.

Lean

Nat.gcd is defined by the recursion of Euclid. The facts Nat.gcd_dvd_left and Nat.gcd_dvd_right say that the gcd divides both numbers, and Nat.dvd_gcd : k ∣ m → k ∣ n → k ∣ Nat.gcd m n says that it is the greatest in the order of divisibility. The identity of Bezout is Nat.gcd_eq_gcd_ab. A computation such as Nat.gcd 544 119 = 17 is closed by by norm_num or by decide.

Notes and history

Euclid's algorithm is in Book VII of the Elements, written about 300 BC, and it is the oldest algorithm that is still in use in nearly its original form. TAOCP uses it as the first algorithm in Section 1.1, and in Section 1.2.1 it proves the extended form correct by the method of assertions at the arrows, which Chapter 8 teaches. The identity of Bezout is named after Bezout (1779), who proved it for polynomials; Bachet had it for integers in 1624. The theorem of Lame is the first analysis of the running time of an algorithm.

The induction of Chapter 4 reaches P(k+1)P(k + 1) from P(k)P(k) alone. Many claims need more: the proof that a number has a prime factorization splits the number into two smaller numbers that need not be kk. This chapter gives the principle that fits such proofs, strong induction, and the principle that is equivalent to it, the well ordering of the natural numbers, which gives the method of a least counterexample.

Section 1 states strong induction, shows that it follows from weak induction, and uses it for the postage problem, the binary representation and the existence of a prime factorization. Section 2 states the well-ordering principle, shows that the three principles imply each other, proves the division theorem with it, and shows that the loop of Euclid's algorithm must stop. Section 3 builds the greatest common divisor on the division theorem, proves the identity of Bezout, and proves that Algorithm 1.1E of TAOCP computes the greatest common divisor, by strong induction on nn. It ends with the extended algorithm, as a preview of the invariants of Chapter 8.

The chapter assumes Chapters 1 to 4. The spine texts are Velleman, How to Prove It, Section 6.4, and Lehman, Leighton and Meyer, Mathematics for Computer Science, Chapters 5 and 6. The prime numbers are the subject of the number-theory volumes, and only their first facts are proved here.

The source of this page. Something is wrong? Report it.