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
Strong induction
Statement
Theorem P1.5.1 (the principle of strong induction). Let be a statement about the natural number , and let be a natural number. If for every the following is true,
then holds for every .
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 has no with , so it says that 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 and assume for all , and prove . 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 be the statement " for all with ", for (for the statement is empty and true). Base: is true. Step: assume for some , that is, for all . This is the hypothesis of the principle for the number , so holds, and with this is . By induction holds for all , so holds for all .
The proof shows that the two principles have the same strength: weak induction is the special case of strong induction that uses only .
Example P1.5.3 (postage). Claim: every amount of or more can be paid with stamps of and . Strong induction with . The amounts , , and are cases of a base. For the amount can be paid by the hypothesis, and one more stamp of pays . The amounts cannot be paid, which a computer search confirms, so the claim is best possible: is not payable.
Example P1.5.4 (the binary representation). Claim: every integer is a sum of distinct powers of . Let and assume the claim for every with . If , it is done. Otherwise let be the largest power of with . If , it is done. If not, satisfies (since ), and . By the hypothesis, is a sum of distinct powers of , all below , since their sum is below . Adding gives . For .
Theorem P1.5.5 (a prime divisor). Every integer has a prime divisor.
Proof. A prime is an integer whose only positive divisors are and . Let and assume the claim for every with . If is prime, then divides . If not, then with and . By the hypothesis, has a prime divisor , and .
Theorem P1.5.6 (existence of a prime factorization). Every integer 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 and assume the claim for all with . If is prime, the product has one factor. If not, with , and by the hypothesis and are products of primes. The product of the two lists is a list for .
Example P1.5.7. and . A proof by weak induction would need to give the factorization of the factors and of , which are not .
Example P1.5.8 (a recurrence that refers to half). Let and for . Claim: , which is the number of digits of in base . Let and assume the claim for . Since , . If , then , so and . For , .
Proof
The principle of strong induction was proved above. A proof by it follows a plan.
- Say that the induction is strong, with the statement and the first value .
- Let be arbitrary and assume for all .
- Prove , with cases if needed: a case for the small that has no usable , and a case for the larger that reduces to smaller values.
- Check that each smaller value used is at least and less than .
The usual error is at step 4. A proof that uses for all is valid only for , and the cases and need their own proof. The example of postage has four base cases for this reason: the step uses , so the first four values must be done by hand.
Examples
Example P1.5.9 (every with stamps of and ). Strong induction with the base cases , and , and the step for (use ). The amounts that cannot be paid are .
Example P1.5.10 (Zeckendorf's representation). Claim: every integer is a sum of Fibonacci numbers with distinct indices at least . Let and assume the claim for . Let be the largest Fibonacci number with and . If , done. Otherwise , and gives , so the numbers of the representation of have indices at most , and can be added with a different index. For , the largest is , and .
Example P1.5.11 (a false proof). Claim: every integer is even. Strong induction: let , and assume the claim for . Write , a sum of two even numbers. The step is false for , since , and for there is no base case. The claim is false for .
Exercises
- [10] Show that every amount of or more can be paid with stamps of and , and list the amounts that cannot.
- [12] Show that every amount of or more can be paid with stamps of and , and list the amounts that cannot.
- [14] Prove that every integer is a sum of distinct powers of , and write in this form.
- [14] Prove that every integer has a prime divisor, and find the least prime divisor of and of .
- [15] Prove that every integer is a product of primes, and write and as products of primes.
- [16] Let and . Prove that for , and find .
- [M20] Prove that every integer is a sum of Fibonacci numbers with distinct indices at least , and write in this form.
- [M20] Prove that the number of prime factors of , counted with multiplicity, is at most .
- [M22] Show that strong induction follows from weak induction by the statement of the text, and show that weak induction follows from strong induction.
- [M25] Find the error: "Claim: every integer is even. Proof: for , 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 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 to use all of , which is the strong form.
Well ordering
Statement
Definition P1.5.12 (least element). Let be a set of integers. An element is a least element of if for every . A least element is unique: if and are both least, then and .
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 the number 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 be a set of natural numbers with no least element, and let be "". Let be a natural number and assume for all . If , then is a least element, since no smaller number is in , which contradicts the choice of . So . By strong induction has no element, that is, is empty. So a nonempty set has a least element. Well ordering implies weak induction: this is the proof of Chapter 4, Section 1.
Definition P1.5.14 (the method of the least counterexample). To prove for all by well ordering, suppose it is false for some . The set is nonempty, so it has a least element . Show that cannot exist: either is true after all, or a smaller element of exists.
Example P1.5.15. Claim: for . Suppose it is false, and let be the least with . Since , . The number is smaller than , so , which gives . Then , which contradicts .
Theorem P1.5.16 (the division theorem). Let and be integers with . There are unique integers and with and .
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 is nonempty: for the number . By well ordering has a least element . If , then is in and smaller than , a contradiction. So .
Proof of uniqueness. Let with . Then , and . The multiple of with absolute value less than is . So and .
The number is the quotient and is the remainder. For and : , so and , and the remainder is not negative. The numbers are and in TAOCP (Section 1.2.4).
Proof
The division theorem is the first use of well ordering that makes a number. The set of the proof is the set of all values 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 and be natural numbers with . Consider the repeated step: let be the remainder of divided by ; if stop; otherwise replace by . The steps cannot go on forever.
Proof. Suppose they go on forever, with the values of the second number before each step. Each is a remainder of a division by , so (it is not , since the algorithm did not stop). The set of natural numbers is nonempty, so it has a least element . But is in the set, a contradiction.
The proof gives also a bound: the number of steps is at most , since each step lowers the second number by at least and it stays positive. The bound is poor, and Section 3 gives a good one.
Examples
Example P1.5.18 ( by least denominator). Suppose is rational. Then the set of positive integers such that is an integer is nonempty. Let be its least element and . Since , . Let . Then , and is an integer. So is smaller than , a contradiction. This is a proof without the notion of lowest terms.
Example P1.5.19 (a greatest element). Claim: a nonempty set of natural numbers with an upper bound has a greatest element. The set is a nonempty set of natural numbers, so it has a least element . Then is the greatest element of , since gives for every .
Example P1.5.20 (the number of digits). For each integer the set of with is nonempty, so it has a least element , and . This is the number of digits of in base . For it is , and for it is .
Exercises
- [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.
- [12] Find the quotient and remainder of the division of by for , , and .
- [14] Prove by the method of the least counterexample that for .
- [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 is replaced by .
- [15] Prove that strong induction implies the well-ordering principle.
- [M18] Prove that there is no integer with , by well ordering.
- [M20] Prove by well ordering that is irrational, with the set of positive integers such that is an integer.
- [M20] Prove that every nonempty set of natural numbers that has an upper bound has a greatest element.
- [M22] Let the steps of Euclid's algorithm on with be run. Prove that the remainders strictly decrease, and that the second number is halved at least every two steps: .
- [M25] Let be a set of natural numbers with , and with whenever all of . Prove by well ordering that 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 let be the set of positive divisors of . A common divisor of and is an element of . If and are not both , the set is finite, and its greatest element is the greatest common divisor . By convention , and every positive integer divides .
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. and . The common divisors are , so . Also for , since each divisor of divides .
Lemma P1.5.23 (the key lemma of Euclid). If for integers , then and have the same common divisors as and . So , and when these are defined.
Proof. Let and . Then (Chapter 3). Conversely, let and . Then . So a number divides both and iff it divides both and .
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 and be positive integers. Euclid's algorithm is:
- E1. Divide by and let be the remainder.
- E2. If , the algorithm ends and is the answer.
- E3. Set and , and go back to E1.
It ends, and the answer is .
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 the statement : for every positive , the algorithm started on ends with the answer . Let and assume for . Let and let be the remainder of by , so and . If , the algorithm answers , and , so the common divisors of and are the divisors of , with greatest element . If , the algorithm continues on with , and by the hypothesis it ends with the answer . By the key lemma .
Example P1.5.25. For and the steps are , , , . The answer is , in four divisions. For the answer is in five divisions.
Theorem P1.5.26 (the identity of Bezout). For integers and , not both , there are integers and with .
Lean: Nat.gcd_eq_gcd_ab : (Nat.gcd x y : ℤ) = x * Nat.gcdA x y + y * Nat.gcdB x y (Mathlib).
Proof. The set of positive integers of the form is nonempty, since it holds . By well ordering let be its least element. To show that divides , use the division theorem: with . Then has the form , and if it would be an element of smaller than . So and . The same proof gives . Thus is a common divisor, and . Conversely, divides , so . Hence .
Corollary P1.5.27. A common divisor of and divides . The equation has an integer solution iff . Two integers are coprime, with , iff has a solution.
Proof. A common divisor divides . If , take the solution , . If , then divides since it divides and .
Lemma P1.5.28 (Euclid's lemma). If and , then . If is a prime and , then or .
Proof. Let . Then . Since and , it follows that . For the prime, if , then since the only positive divisors of are and , and the first part applies.
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 : but and .
Proof
The extended algorithm finds the numbers and 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 , and keep four more integers with the invariant
At the start and , so take . In a pass let be the quotient of by . The new pair is , and by the invariant . So the new coefficients are and the invariant holds again. When , the answer is .
Example P1.5.29. For the quotients are and the rows are
The answer is . Check: .
Example P1.5.30 (the inverse modulo a number). If , the numbers of Bezout satisfy , so and is an inverse of modulo . For and the extended algorithm gives , so is the inverse of modulo .
Examples
Example P1.5.31 (the worst case). For consecutive Fibonacci numbers, Euclid's algorithm is slowest: takes divisions for , since with gives the quotient each time. For the sequence is , which is divisions (). The theorem of Lame of 1844 says that if the algorithm on takes divisions, then and , so is about .
Example P1.5.32 (coprime pairs). The number of pairs with and is by a computer count, a little over percent of the pairs.
Example P1.5.33 (a divisor that ends the algorithm at once). For the first division gives , so the answer is after one division. The numbers of Bezout are and .
Exercises
- [10] Run Euclid's algorithm on and on , and count the divisions.
- [12] Find and numbers and with .
- [14] Prove the key lemma: if then .
- [14] Find the inverse of modulo and the inverse of modulo .
- [15] Prove that for .
- [M18] Prove that if and then .
- [M20] Prove that and imply .
- [M22] Prove that , and that the algorithm on them takes divisions for .
- [M25] Prove the uniqueness of the prime factorization by strong induction, using Euclid's lemma.
- [M25] Prove that has an integer solution iff , and find all solutions of .
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 from 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 . 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 . 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.