taocp

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

Volume C1 · Basic concepts · TAOCP Section 1.2.1

Mathematical induction

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 it next to Section 1.2.1 of the book. The nodes and the exercises of the section.

Contents
  1. Proof by induction and proofs of algorithms

Proof by induction and proofs of algorithms

Overview

Section 1.2.1 of TAOCP states the principle of proof by induction, in the form where the step from nn to n+1n + 1 may use all the cases 1,…,n1, \ldots, n. It proves two facts with it: the sum of the first nn odd numbers is n2n^2, and the Fibonacci number FnF_n is at most ϕn−1\phi^{n-1}. It writes induction as an algorithm, Algorithm 1.2.1I, and it shows with the numbers of partitions that a pattern in small cases is not a proof. Then it turns to algorithms. It extends Euclid's algorithm of Section 1.1 so that it also finds integers aa and bb with am+bn=gcd⁡(m,n)am + bn = \gcd(m, n), and it proves this Algorithm 1.2.1E by an invariant. It makes the proof a general method: put an assertion on each arrow of a flow chart and check each box. It ends with two warnings and a foundation: a proof of the assertions does not prove that the algorithm stops, and the principle of induction is the same as the fact that each nonempty set of positive integers has a least member. The section uses Section 1.1 (Algorithm 1.1E and the idea of a computational method).

The ideas

Induction proves infinitely many statements with two proofs. Let P(n)P(n) be a statement about the positive integer nn. To prove P(n)P(n) for all nn, prove P(1)P(1), and prove that P(1),…,P(n)P(1), \ldots, P(n) together give P(n+1)P(n + 1). Then P(1)P(1) gives P(2)P(2), these two give P(3)P(3), and so on: each single case has a proof of finite length. The second part may use all the earlier cases, not only P(n)P(n). The Fibonacci bound below needs two earlier cases, and many proofs of the book need more.

A guess is not a proof. The word "induction" in the sciences means a general rule found from many cases. In mathematics, a rule that holds for n=1,…,6n = 1, \ldots, 6 can fail at n=7n = 7. The numbers of partitions show this (Example C1.1.2.1.7 below). In this book "induction" always means proof by induction.

An invariant is induction on the steps of an algorithm. To prove that an algorithm is correct, find a relation among its variables that is true at the start and that each step keeps true. Then it is true after any number of steps, by induction on the number of steps. At the end, the relation and the stop condition together give the answer. For the extended Euclid algorithm the relations are two linear equations: each of the two current numbers is a combination of mm and nn with integer coefficients, and the algorithm keeps the coefficients up to date.

Assertions on a flow chart make this a method for any algorithm. Write a property of the state on each arrow of the flow chart. If the property on the first arrow holds for each input, and each box turns a true property on an arrow in into a true property on the arrow out, then each property holds in each run. The hard part is to find the right properties. Once they are found, the proof is a list of small checks, one for each box.

Correctness and termination are two proofs. The assertions say what is true if the run comes to a given arrow. They do not say that the run comes to the end. Algorithm 1.2.1E with real numbers instead of integers keeps all its linear invariants and can run for ever. A proof that an algorithm stops needs a quantity that gets smaller at each step in a set with no infinite decreasing sequence, such as the nonnegative integers.

C1 proves the principle of induction from the well-ordering of the positive integers, and the well-ordering from induction, so that the reader sees that the two are the same axiom. The book takes induction as the axiom and does not give the first proof. The proof of Algorithm 1.2.1E in C1 is by the invariant of eq. 1.2.1(6) and a separate proof of termination. The book also gives a second proof by induction on nn.

Definitions, results and proofs

Theorem C1.1.2.1.1 (the principle of induction, TAOCP Section 1.2.1). Let P(n)P(n) be a statement for each positive integer nn. Suppose that (a) P(1)P(1) is true, and (b) for each n≥1n \ge 1, if P(1),P(2),…,P(n)P(1), P(2), \ldots, P(n) are all true, then P(n+1)P(n + 1) is true. Then P(n)P(n) is true for each positive integer nn.

Node: TANDZ7GDS1 · status: not stated yet

Explanation. Part (a) is the base and part (b) is the step. In the simple form of induction the step uses only P(n)P(n). The form here, often called strong induction, lets the step use all the earlier cases. The two forms prove the same statements: to get the strong form from the simple one, apply the simple form to the statement Q(n)Q(n) = "P(1),…,P(n)P(1), \ldots, P(n) are all true". The book uses induction on n≥0n \ge 0 as often as on n≥1n \ge 1. The change is to start the base at 00 (exercise 1.2.1-1 is about this change). In Lean the simple form is the recursor Nat.rec and the strong form is Nat.strong_induction_on of Mathlib.

Example. Each positive integer is a sum of distinct powers of 22. Let P(n)P(n) be this statement. P(1)P(1) is true, since 1=201 = 2^0. For the step, let n≥1n \ge 1, suppose that P(1),…,P(n)P(1), \ldots, P(n) are true, and let 2j2^j be the largest power of 22 with 2j≤n+12^j \le n + 1. If n+1=2jn + 1 = 2^j, then P(n+1)P(n + 1) is true. If not, then 1≤n+1−2j<2j1 \le n + 1 - 2^j < 2^j, since n+1<2j+1n + 1 < 2^{j+1}. By P(n+1−2j)P(n + 1 - 2^j), the number n+1−2jn + 1 - 2^j is a sum of distinct powers of 22, and each of them is less than 2j2^j. Add 2j2^j to get n+1n + 1. For n+1=13n + 1 = 13: the largest power is 88, the case 13−8=513 - 8 = 5 gives 5=4+15 = 4 + 1, and so 13=8+4+113 = 8 + 4 + 1. The step uses P(5)P(5), not P(12)P(12), so it needs the strong form.

Proof. The proof uses the well-ordering of the positive integers (Theorem C1.1.2.1.12 below). Suppose that P(n)P(n) is false for some nn, and let SS be the set of the positive integers nn for which P(n)P(n) is false. Then SS is not empty, so it has a least member n0n_0. By (a), n0≠1n_0 \ne 1, so n0≥2n_0 \ge 2. Each of 1,…,n0−11, \ldots, n_0 - 1 is less than n0n_0, so it is not in SS, and P(1),…,P(n0−1)P(1), \ldots, P(n_0 - 1) are true. By (b) with n=n0−1n = n_0 - 1, P(n0)P(n_0) is true. This is false, since n0∈Sn_0 \in S. So SS is empty, and P(n)P(n) is true for each nn. □\square

Example C1.1.2.1.2 (the first sums of odd numbers, TAOCP eq. 1.2.1(1)). The sums of the first few odd numbers are squares:

1=12,1+3=4=22,1+3+5=9=32,1+3+5+7=16=42,1+3+5+7+9=25=52.1 = 1^2, \quad 1 + 3 = 4 = 2^2, \quad 1 + 3 + 5 = 9 = 3^2, \quad 1 + 3 + 5 + 7 = 16 = 4^2, \quad 1 + 3 + 5 + 7 + 9 = 25 = 5^2.

Node: TADCCDXQPR · status: proved

The next one is 25+11=36=6225 + 11 = 36 = 6^2. Each line is a fact that a person can check. The five lines together suggest the general rule of eq. 1.2.1(2), but they do not prove it. The proof is the next theorem.

Theorem C1.1.2.1.3 (the sum of the odd numbers, TAOCP eq. 1.2.1(2)). For each positive integer nn,

1+3+5+⋯+(2n−1)=∑k=1n(2k−1)=n2.1 + 3 + 5 + \cdots + (2n - 1) = \sum_{k=1}^{n} (2k - 1) = n^2.

Node: TAV92J4EQX · status: proved

Explanation. The sum of the first nn odd numbers is a square. A picture explains it: an n×nn \times n square of cells is a 1×11 \times 1 square with n−1n - 1 L-shaped layers around it, and the layer that makes the k×kk \times k square from the (k−1)×(k−1)(k-1) \times (k-1) square has 2k−12k - 1 cells. The picture is a proof only when it is drawn for each nn, and that is the induction below. The theorem is the first example of the simple form of induction, where the step uses only P(n)P(n).

Example. For n=7n = 7: 1+3+5+7+9+11+13=49=721 + 3 + 5 + 7 + 9 + 11 + 13 = 49 = 7^2. For n=10n = 10 the last term is 1919, and the sum is 100100.

Proof. Let P(n)P(n) be the statement. For n=1n = 1, the sum is 1=121 = 1^2. Suppose P(n)P(n) for some n≥1n \ge 1. The sum of the first n+1n + 1 odd numbers is the sum of the first nn, plus 2(n+1)−1=2n+12(n + 1) - 1 = 2n + 1. By P(n)P(n) this is n2+2n+1=(n+1)2n^2 + 2n + 1 = (n + 1)^2, which is P(n+1)P(n + 1). By the principle of induction, P(n)P(n) is true for each n≥1n \ge 1. □\square

A second proof: write the sum SS forwards and backwards and add the two. The term 2k−12k - 1 meets the term 2(n+1−k)−12(n + 1 - k) - 1, and the sum of the two is 2n2n. There are nn such pairs, so 2S=n⋅2n2S = n \cdot 2n and S=n2S = n^2. This proof is not by an explicit induction, but the rule that lets a finite sum be written in a different order is itself proved by induction (Section 1.2.3).

Theorem C1.1.2.1.4 (an upper bound of the Fibonacci numbers, TAOCP eq. 1.2.1(3)). Let F0=0F_0 = 0, F1=1F_1 = 1 and Fn+1=Fn+Fn−1F_{n+1} = F_n + F_{n-1} for n≥1n \ge 1, and let ϕ=(1+5)/2\phi = (1 + \sqrt5)/2. Then

Fn≤ϕn−1for each integer n≥1.F_n \le \phi^{n-1} \qquad \text{for each integer } n \ge 1.

Node: TAY3BR964P · status: proved

Explanation. The Fibonacci numbers grow, but not faster than the powers of the golden ratio ϕ≈1.618\phi \approx 1.618. The bound is the first of many facts of the book about FnF_n and ϕ\phi. Section 1.2.8 proves the exact formula Fn=(ϕn−ϕ^n)/5F_n = (\phi^n - \hat\phi^n)/\sqrt5, and Section 4.5.3 uses the bound to find the worst case of Euclid's algorithm. The proof needs the strong form of induction, since Fn+1F_{n+1} depends on two earlier values.

Example. The values for n=1,…,10n = 1, \ldots, 10, with ϕn−1\phi^{n-1} to three decimals:

nn 1 2 3 4 5 6 7 8 9 10
FnF_n 1 1 2 3 5 8 13 21 34 55
ϕn−1\phi^{n-1} 1 1.618 2.618 4.236 6.854 11.090 17.944 29.034 46.979 76.013

The bound is an equality only at n=1n = 1, and the ratio Fn/ϕn−1F_n/\phi^{n-1} tends to ϕ/5≈0.724\phi/\sqrt5 \approx 0.724.

Proof. Let P(n)P(n) be the statement Fn≤ϕn−1F_n \le \phi^{n-1}. For n=1n = 1: F1=1=ϕ0F_1 = 1 = \phi^0. For the step, let n≥1n \ge 1 and suppose that P(1),…,P(n)P(1), \ldots, P(n) are true. If n=1n = 1, then P(n+1)P(n + 1) is F2=1≤ϕF_2 = 1 \le \phi, which is true since ϕ>1\phi > 1. If n≥2n \ge 2, then P(n−1)P(n - 1) and P(n)P(n) give the bound Fn+1≤ϕn−2(1+ϕ)F_{n+1} \le \phi^{n-2}(1 + \phi) of eq. 1.2.1(4), and 1+ϕ=ϕ21 + \phi = \phi^2 by eq. 1.2.1(5). So Fn+1≤ϕn−2ϕ2=ϕnF_{n+1} \le \phi^{n-2}\phi^2 = \phi^n, which is P(n+1)P(n + 1). By the principle of induction, P(n)P(n) holds for each n≥1n \ge 1. □\square

Theorem C1.1.2.1.5 (the step of the Fibonacci bound, TAOCP eq. 1.2.1(4)). Let n≥2n \ge 2. If Fn−1≤ϕn−2F_{n-1} \le \phi^{n-2} and Fn≤ϕn−1F_n \le \phi^{n-1}, then

Fn+1=Fn−1+Fn≤ϕn−2+ϕn−1=ϕn−2(1+ϕ).F_{n+1} = F_{n-1} + F_n \le \phi^{n-2} + \phi^{n-1} = \phi^{n-2}(1 + \phi).

Node: TAH47VRQWJ · status: proved

Explanation. This is the core of the step of the proof of eq. 1.2.1(3). It uses two earlier cases, and it is the reason why the proof needs two base cases, n=1n = 1 and n=2n = 2. With only the case n=1n = 1, the step from 11 to 22 would need F0≤ϕ−1F_0 \le \phi^{-1}, which is true but is not part of the statement.

Example. For n=5n = 5: F4=3≤ϕ3≈4.236F_4 = 3 \le \phi^3 \approx 4.236 and F5=5≤ϕ4≈6.854F_5 = 5 \le \phi^4 \approx 6.854. So F6=8≤4.236+6.854=11.090F_6 = 8 \le 4.236 + 6.854 = 11.090, and ϕ3(1+ϕ)≈4.236×2.618≈11.090\phi^3(1 + \phi) \approx 4.236 \times 2.618 \approx 11.090.

Proof. The equation Fn+1=Fn−1+FnF_{n+1} = F_{n-1} + F_n is the rule of the sequence. Add the two given inequalities to get Fn−1+Fn≤ϕn−2+ϕn−1F_{n-1} + F_n \le \phi^{n-2} + \phi^{n-1}. The last equation is the distributive law, ϕn−2+ϕn−1=ϕn−2⋅1+ϕn−2⋅ϕ\phi^{n-2} + \phi^{n-1} = \phi^{n-2} \cdot 1 + \phi^{n-2} \cdot \phi. □\square

Theorem C1.1.2.1.6 (the equation of the golden ratio, TAOCP eq. 1.2.1(5)). The number ϕ=(1+5)/2\phi = (1 + \sqrt5)/2 satisfies

ϕ2=ϕ+1.\phi^2 = \phi + 1.

Node: TA3K7PC743 · status: proved

Explanation. The golden ratio is the positive root of x2=x+1x^2 = x + 1. The other root is ϕ^=(1−5)/2≈−0.618\hat\phi = (1 - \sqrt5)/2 \approx -0.618. The equation is the reason why the powers of ϕ\phi behave like the Fibonacci numbers: multiply it by ϕn−1\phi^{n-1} to get ϕn+1=ϕn+ϕn−1\phi^{n+1} = \phi^n + \phi^{n-1}, the rule of the Fibonacci sequence. In Mathlib the number is Real.goldenRatio and the equation is Real.goldenRatio_sq.

Example. With ϕ≈1.6180340\phi \approx 1.6180340: ϕ2≈2.6180340\phi^2 \approx 2.6180340 and ϕ+1≈2.6180340\phi + 1 \approx 2.6180340. Also 1/ϕ=ϕ−1≈0.61803401/\phi = \phi - 1 \approx 0.6180340, which is the equation divided by ϕ\phi.

Proof. ϕ2=(1+25+5)/4=(6+25)/4=(3+5)/2=1+(1+5)/2=1+ϕ\phi^2 = (1 + 2\sqrt5 + 5)/4 = (6 + 2\sqrt5)/4 = (3 + \sqrt5)/2 = 1 + (1 + \sqrt5)/2 = 1 + \phi. □\square

Example C1.1.2.1.7 (the numbers of partitions, TAOCP Section 1.2.1). Let p(n)p(n) be the number of ways to write nn as a sum of positive integers when the order of the terms does not count. The first values are

p(1)=1,p(2)=2,p(3)=3,p(4)=5,p(5)=7,p(6)=11,p(7)=15.p(1) = 1, \quad p(2) = 2, \quad p(3) = 3, \quad p(4) = 5, \quad p(5) = 7, \quad p(6) = 11, \quad p(7) = 15.

The values p(2),…,p(6)p(2), \ldots, p(6) are the first five primes, 2,3,5,7,112, 3, 5, 7, 11, so a guess from these cases is "p(n+1)p(n + 1) is the nn-th prime". The value p(7)p(7) breaks the guess: 15=3⋅515 = 3 \cdot 5 is not prime, and the sixth prime is 1313. To check p(6)=11p(6) = 11, here are the partitions of 66:

6, 5+1, 4+2, 4+1+1, 3+3, 3+2+1, 3+1+1+1, 2+2+2, 2+2+1+1, 2+1+1+1+1, 1+1+1+1+1+1.6,\ 5{+}1,\ 4{+}2,\ 4{+}1{+}1,\ 3{+}3,\ 3{+}2{+}1,\ 3{+}1{+}1{+}1,\ 2{+}2{+}2,\ 2{+}2{+}1{+}1,\ 2{+}1{+}1{+}1{+}1,\ 1{+}1{+}1{+}1{+}1{+}1.

The point of the example is the difference between a guess and a proof. Five correct cases make a good guess, and they are a reason to look for a proof. Only a proof for all nn, by induction or another method, makes the guess a theorem. Section 7.2.1.4 of the book makes all the partitions of nn and has much more on p(n)p(n).

Theorem C1.1.2.1.8 (the two invariants of the extended Euclid algorithm, TAOCP eq. 1.2.1(6)). In Algorithm 1.2.1E (in the part Algorithms and programs below), each time a pass of the loop starts,

xm+yn=u,sm+tn=v.x m + y n = u, \qquad s m + t n = v.

Node: TA93Q8DJED · Lean: TAOCP.S1_2_1.Eq6 (partial) · status: not stated yet

Explanation. The algorithm keeps two numbers uu and vv, which are the pair of Euclid's algorithm: they start as mm and nn, and each pass replaces (u,v)(u, v) by (v,u mod v)(v, u \bmod v). With each of them it keeps two integer coefficients that write it as a combination of mm and nn. At the end, v=gcd⁡(m,n)v = \gcd(m, n), and the coefficients ss and tt of vv are the integers aa and bb that the algorithm must find. In the notation of the book, uu and vv are cc and dd, (x,y)(x, y) is (a′,b′)(a', b'), and (s,t)(s, t) is (a,b)(a, b), so the two equations are eq. 1.2.1(6) of the book.

Example. In the worked run of Algorithm 1.2.1E on m=99m = 99, n=78n = 78 below, at the start of the third pass, (u,x,y)=(21,1,−1)(u, x, y) = (21, 1, -1) and (v,s,t)=(15,−3,4)(v, s, t) = (15, -3, 4). The first invariant is 1⋅99−1⋅78=211 \cdot 99 - 1 \cdot 78 = 21, and the second is −3⋅99+4⋅78=−297+312=15-3 \cdot 99 + 4 \cdot 78 = -297 + 312 = 15.

Proof. By induction on the number of passes. At the start of the first pass, (u,x,y)=(m,1,0)(u, x, y) = (m, 1, 0) and (v,s,t)=(n,0,1)(v, s, t) = (n, 0, 1), so the equations are 1⋅m+0⋅n=m1 \cdot m + 0 \cdot n = m and 0⋅m+1⋅n=n0 \cdot m + 1 \cdot n = n, which are true. Suppose the equations hold at the start of a pass that does not return, with q=⌊u/v⌋q = \lfloor u/v \rfloor and r=u−qvr = u - qv. The pass sets the new (u,x,y)(u, x, y) to the old (v,s,t)(v, s, t), so the new first equation is the old second equation. It sets the new (v,s,t)(v, s, t) to (r,x−qs,y−qt)(r, x - qs, y - qt), and

(x−qs)m+(y−qt)n=(xm+yn)−q(sm+tn)=u−qv=r.(x - qs)m + (y - qt)n = (xm + yn) - q(sm + tn) = u - qv = r.

So both equations hold at the start of the next pass. □\square

The proof is exercise 1.2.1-6 in the notation of this chapter. It uses only that qq is an integer, and it never uses that rr is the remainder. This fact comes back in Example C1.1.2.1.11 below.

Definition C1.1.2.1.9 (the condition on a box of a flow chart, TAOCP eq. 1.2.1(7)). Let a flow chart have an assertion AeA_e, a property of the state, on each arrow ee. A box of the flow chart satisfies the condition when, for each arrow ee into the box and each state σ\sigma for which AeA_e is true, the box, run on σ\sigma, gives a state σ′\sigma' and leaves along an arrow e′e' for which Ae′(σ′)A_{e'}(\sigma') is true.

Node: TAE9RB7VFA · status: not stated yet

Explanation. The condition is local: it is about one box and its arrows, not about the whole run. So it can be checked one box at a time, by algebra on the operation of the box. The arrows out of a test box have different assertions, and the condition asks for the assertion of the arrow that the test takes. The condition must hold for each arrow into the box, so a box with two arrows in has two checks, or one check with the weaker of the two assertions.

Example. Take the division box of Algorithm 1.2.1E, which sets q←⌊u/v⌋q \gets \lfloor u/v \rfloor and r←u mod vr \gets u \bmod v. Let the assertion into it be "the invariants of eq. 1.2.1(6) hold, u≥1u \ge 1, v≥1v \ge 1 and gcd⁡(u,v)=gcd⁡(m,n)\gcd(u, v) = \gcd(m, n)", and let the assertion out of it be the same, plus "u=qv+ru = qv + r and 0≤r<v0 \le r < v". The box changes only qq and rr, so the first part stays true. Since v≥1v \ge 1, the division is defined and gives u=qv+ru = qv + r with 0≤r<v0 \le r < v. So the box satisfies the condition. The part v≥1v \ge 1 of the assertion in is necessary: without it the division by vv has no meaning.

Theorem C1.1.2.1.10 (the method of assertions, TAOCP Section 1.2.1). Let a flow chart have an assertion on each arrow. Suppose that the assertion on the arrow from the start is true for each input, and that each box satisfies the condition of eq. 1.2.1(7). Then in each run, each time the run goes along an arrow, the assertion on that arrow is true of the state at that time.

Node: TAY9MWQ1B1 · status: not stated yet

Explanation. This is the general method of proof of an algorithm. It reduces a statement about all runs, which can be long, to one check for each box, which is short. The assertion on the arrow into the stop box gives the correctness: if it says "the output is the right answer", the algorithm is correct whenever it stops. The theorem says nothing about the stop itself (see Example C1.1.2.1.11). The method is due to R. W. Floyd (see the notes), and it is the base of the logic of programs that P7 teaches.

Example. The flow chart of Algorithm 1.2.1E has an initial box, a division box, a test and an update box. Let JJ be the assertion "xm+yn=uxm + yn = u, sm+tn=vsm + tn = v, u≥1u \ge 1, v≥1v \ge 1 and gcd⁡(u,v)=gcd⁡(m,n)\gcd(u, v) = \gcd(m, n)". Put these assertions on the arrows:

  1. start to the initial box: m≥1m \ge 1 and n≥1n \ge 1;
  2. the initial box to the division box, and the update box to the division box: JJ;
  3. the division box to the test: JJ, u=qv+ru = qv + r and 0≤r<v0 \le r < v;
  4. the test to the stop, when r=0r = 0: JJ and vv divides uu;
  5. the test to the update box, when r>0r > 0: JJ, u=qv+ru = qv + r and 0<r<v0 < r < v;
  6. the stop: v=gcd⁡(m,n)v = \gcd(m, n) and sm+tn=vsm + tn = v.

The checks of the boxes: the initial box sets (u,x,y)=(m,1,0)(u, x, y) = (m, 1, 0) and (v,s,t)=(n,0,1)(v, s, t) = (n, 0, 1), and then JJ holds by the base of the proof of eq. 1.2.1(6). The division box is the example of eq. 1.2.1(7). The test with r=0r = 0 gives u=qvu = qv, so vv divides uu; with r>0r > 0 it gives assertion 5. The update box gives JJ for the new values by the step of the proof of eq. 1.2.1(6), by r≥1r \ge 1, and by the lemma gcd⁡(u,v)=gcd⁡(v,r)\gcd(u, v) = \gcd(v, r) of Algorithm 1.1E. At the stop, vv divides uu, so gcd⁡(u,v)=v\gcd(u, v) = v, and JJ gives v=gcd⁡(m,n)v = \gcd(m, n) and sm+tn=vsm + tn = v. By the theorem, assertion 6 is true each time the run stops.

Proof. Number the arrows of a run e1,e2,e3,…e_1, e_2, e_3, \ldots in the order in which the run goes along them, and let σi\sigma_i be the state when it goes along eie_i. Let P(i)P(i) be the statement "Aei(σi)A_{e_i}(\sigma_i) is true". The arrow e1e_1 is the arrow from the start, and its assertion is true for each input, so P(1)P(1) is true. Suppose P(i)P(i). The run goes from eie_i into a box, the box runs on σi\sigma_i, and the run leaves along ei+1e_{i+1} with the state σi+1\sigma_{i+1}. Since the box satisfies the condition of eq. 1.2.1(7) and Aei(σi)A_{e_i}(\sigma_i) is true, Aei+1(σi+1)A_{e_{i+1}}(\sigma_{i+1}) is true. This is P(i+1)P(i + 1). By induction, P(i)P(i) is true for each ii for which the run has an ii-th arrow. □\square

Example C1.1.2.1.11 (a run that keeps its assertions and never stops, TAOCP Section 1.2.1). The steps of Algorithm 1.2.1E make sense when mm and nn are positive real numbers and q=⌊u/v⌋q = \lfloor u/v \rfloor is still an integer. Take m=ϕ=(1+5)/2m = \phi = (1 + \sqrt5)/2 and n=1n = 1. Since u/v=ϕu/v = \phi at each pass, q=1q = 1 at each pass, and the remainders are the powers ϕ−1,ϕ−2,ϕ−3,…\phi^{-1}, \phi^{-2}, \phi^{-3}, \ldots:

Pass uu vv qq r=u−qvr = u - qv new (v,s,t)(v, s, t)
1 ϕ\phi 11 1 ϕ−1=ϕ−1≈0.618\phi - 1 = \phi^{-1} \approx 0.618 (ϕ−1,1,−1)(\phi^{-1}, 1, -1)
2 11 ϕ−1\phi^{-1} 1 1−ϕ−1=ϕ−2≈0.3821 - \phi^{-1} = \phi^{-2} \approx 0.382 (ϕ−2,−1,2)(\phi^{-2}, -1, 2)
3 ϕ−1\phi^{-1} ϕ−2\phi^{-2} 1 ϕ−3≈0.236\phi^{-3} \approx 0.236 (ϕ−3,2,−3)(\phi^{-3}, 2, -3)
4 ϕ−2\phi^{-2} ϕ−3\phi^{-3} 1 ϕ−4≈0.146\phi^{-4} \approx 0.146 (ϕ−4,−3,5)(\phi^{-4}, -3, 5)

Each step uses ϕ−k+1−ϕ−k=ϕ−k(ϕ−1)=ϕ−k−1\phi^{-k+1} - \phi^{-k} = \phi^{-k}(\phi - 1) = \phi^{-k-1}, which is eq. 1.2.1(5) divided by ϕk+1\phi^{k+1}. The remainder is never 00, so the run never stops. But the proof of eq. 1.2.1(6) holds word for word, since it uses only that qq is an integer: for example 2ϕ−3≈0.236=ϕ−32\phi - 3 \approx 0.236 = \phi^{-3}, and the coefficients after pass kk are s=(−1)k+1Fks = (-1)^{k+1}F_k and t=(−1)kFk+1t = (-1)^k F_{k+1}. So the invariants hold at each pass of a run that does not stop. A proof of assertions proves that the answer is right if the algorithm stops, and the stop needs its own proof. For integers the stop comes from the fact that vv is a positive integer that gets smaller at each pass; here vv is a positive real number that gets smaller at each pass, and the positive real numbers have no least member. The book gives a case of the same kind with numbers u+v2u + v\sqrt2, and exercise 1.2.1-12 asks for the details of that case.

Theorem C1.1.2.1.12 (the well-ordering of the positive integers, TAOCP Section 1.2.1). Each nonempty set of positive integers has a least member.

Node: TA3PJREWRP · status: not stated yet

Explanation. The book takes this as the axiom that defines the positive integers, in the form "each positive integer is 11 or comes from a smaller one by adding 11, and so the integers come in order". The proof below gets it from the principle of induction, and the proof of the principle of induction above gets the principle from it, so the two are equivalent. The well-ordering is the tool for the proofs of termination: if each step of an algorithm makes a positive integer smaller, the values form a set with a least member, and the run cannot go on for ever. In Lean the least member is Nat.find, with Nat.find_spec (it is in the set) and Nat.find_min' (it is at most each member).

Example. Let SS be the set of the positive values of 99a+78b99a + 78b for integers aa and bb. It is not empty (99∈S99 \in S), so it has a least member. The extended Euclid run below gives −11⋅99+14⋅78=3-11 \cdot 99 + 14 \cdot 78 = 3, so the least member is at most 33. Each value 99a+78b99a + 78b is a multiple of 3=gcd⁡(99,78)3 = \gcd(99, 78), so no positive value is less than 33. So the least member of SS is 33. The same argument for any mm and nn proves that the least positive value of am+bnam + bn is gcd⁡(m,n)\gcd(m, n).

Proof. Let SS be a set of positive integers with no least member. Let P(n)P(n) be the statement "none of 1,…,n1, \ldots, n is in SS". P(1)P(1) is true: if 1∈S1 \in S, then 11 is the least member of SS, since no positive integer is less than 11. Suppose P(n)P(n). If n+1∈Sn + 1 \in S, then n+1n + 1 is the least member of SS, since no smaller positive integer is in SS by P(n)P(n). So n+1∉Sn + 1 \notin S, and P(n+1)P(n + 1) is true. By induction, P(n)P(n) is true for each nn, so no positive integer is in SS, and SS is empty. So each nonempty set of positive integers has a least member. □\square

Algorithms and programs

Algorithm C1.1.2.1.13 (induction as a procedure, TAOCP Algorithm 1.2.1I). The input is a positive integer nn, a proof of P(1)P(1), and a method that turns proofs of P(1),…,P(k)P(1), \ldots, P(k) into a proof of P(k+1)P(k + 1). The output is a proof of P(n)P(n).

Node: TAJS0SHNZM · status: not stated yet

The idea. The principle of induction is a promise: for each nn, a proof of P(n)P(n) exists. The algorithm makes the promise concrete. It writes the proof of P(1)P(1), then uses the step to write the proofs of P(2),P(3),…P(2), P(3), \ldots, and stops at P(n)P(n).

The steps.

InductionProof(n):                    -- n >= 1
  write a proof of P(1)
  k := 1
  while k < n:
    write a proof of P(k+1) from the proofs of P(1), ..., P(k)
    k := k + 1
  return the last proof               -- a proof of P(n)

A worked run. Take P(n)P(n) to be eq. 1.2.1(2) and n=4n = 4. The step from P(k)P(k) is "add 2k+12k + 1 to both sides, and use k2+2k+1=(k+1)2k^2 + 2k + 1 = (k + 1)^2".

kk Proof written Statement proved
1 1=121 = 1^2 P(1)P(1)
2 1+3=12+3=4=221 + 3 = 1^2 + 3 = 4 = 2^2 P(2)P(2)
3 1+3+5=22+5=9=321 + 3 + 5 = 2^2 + 5 = 9 = 3^2 P(3)P(3)
4 1+3+5+7=32+7=16=421 + 3 + 5 + 7 = 3^2 + 7 = 16 = 4^2 P(4)P(4), then stop

Correctness. The invariant at the test of the loop is "1≤k≤n1 \le k \le n, and proofs of P(1),…,P(k)P(1), \ldots, P(k) are written". It holds the first time, with k=1k = 1. If it holds and k<nk < n, the body writes a proof of P(k+1)P(k + 1) from the proofs that are written, and then increases kk, so it holds again. When the loop ends, k=nk = n, so a proof of P(n)P(n) is written, and it is the last one.

Termination. The quantity n−kn - k is a nonnegative integer that gets smaller by 11 at each pass, so the loop runs exactly n−1n - 1 times.

Cost. The step method runs n−1n - 1 times. If each step adds a proof of bounded length, the proof of P(n)P(n) has length O(n)O(n). So induction gives a proof of finite length for each single case, and a short proof (the base and the step) for all cases at once. The book notes that the algorithm gives a proof of each P(n)P(n), but that to prove that the algorithm itself works for each nn needs induction again. The principle cannot be proved from nothing, and that is why it is an axiom.

In Lean the procedure is the recursor Nat.rec: given the base and the step, Nat.rec gives a proof of P(n)P(n) for each nn, and its computation applies the step nn times. The Lean recursor starts at 00, not at 11.

Algorithm C1.1.2.1.14 (the extended Euclid algorithm, TAOCP Algorithm 1.2.1E). The input is two positive integers mm and nn. The output is d=gcd⁡(m,n)d = \gcd(m, n) and two integers aa and bb with am+bn=dam + bn = d.

Node: TAEDPXG0VB · Lean: TAOCP.S1_2_1.algorithmE · status: not stated yet

The idea. Run Euclid's algorithm on the pair (u,v)(u, v), which starts as (m,n)(m, n). With each of uu and vv, keep integers that write it as a combination of mm and nn. Each new number of Euclid's algorithm is r=u−qvr = u - qv, so its coefficients are the coefficients of uu minus qq times the coefficients of vv. At the end, v=gcd⁡(m,n)v = \gcd(m, n), and its coefficients are the answer.

The steps. The pseudocode of C1 works with two triples. The triple (u,x,y)(u, x, y) means "u=xm+ynu = xm + yn", and the triple (v,s,t)(v, s, t) means "v=sm+tnv = sm + tn".

ExtendedEuclid(m, n):                  -- m >= 1, n >= 1
  (u, x, y) := (m, 1, 0)
  (v, s, t) := (n, 0, 1)
  loop
    q := floor(u / v);  r := u - q*v   -- divide
    if r = 0: return (v, s, t)         -- d = v, a = s, b = t
    (u, x, y), (v, s, t) := (v, s, t), (r, x - q*s, y - q*t)

The last line makes both assignments at once, from the old values. The book's form of the algorithm has four steps and six variables for the coefficients, with a temporary variable for the update; the triples here hold the same values.

A worked run. Take m=99m = 99 and n=78n = 78. Each row is the state at the start of a pass, with the qq and rr of that pass.

Pass (u,x,y)(u, x, y) (v,s,t)(v, s, t) qq rr
1 (99,1,0)(99, 1, 0) (78,0,1)(78, 0, 1) 1 21
2 (78,0,1)(78, 0, 1) (21,1,−1)(21, 1, -1) 3 15
3 (21,1,−1)(21, 1, -1) (15,−3,4)(15, -3, 4) 1 6
4 (15,−3,4)(15, -3, 4) (6,4,−5)(6, 4, -5) 2 3
5 (6,4,−5)(6, 4, -5) (3,−11,14)(3, -11, 14) 2 0

The fifth pass returns (d,a,b)=(3,−11,14)(d, a, b) = (3, -11, 14), and −11⋅99+14⋅78=−1089+1092=3-11 \cdot 99 + 14 \cdot 78 = -1089 + 1092 = 3. The project has no Lean definition of Algorithm 1.2.1E yet. Mathlib's Nat.xgcd computes coefficients of the same kind, and on this input it gives the same pair:

#eval Nat.gcd 99 78    -- 3
#eval Nat.xgcd 99 78   -- (-11, 14)

Correctness. Let JJ be "xm+yn=uxm + yn = u, sm+tn=vsm + tn = v, u≥1u \ge 1, v≥1v \ge 1 and gcd⁡(u,v)=gcd⁡(m,n)\gcd(u, v) = \gcd(m, n)". JJ holds at the start of each pass. The two equations are eq. 1.2.1(6), which is proved above. The rest holds at the first pass since u=mu = m and v=nv = n. At a pass that does not return, the new pair is (v,r)(v, r) with r≥1r \ge 1, and gcd⁡(v,r)=gcd⁡(u,v)\gcd(v, r) = \gcd(u, v) by the lemma of Algorithm 1.1E, so the rest holds at the next pass. When a pass returns, r=0r = 0, so vv divides uu and gcd⁡(u,v)=v\gcd(u, v) = v. By JJ, v=gcd⁡(m,n)v = \gcd(m, n) and sm+tn=vsm + tn = v. So the output (v,s,t)(v, s, t) is right. Theorem C1.1.2.1.10 above sets out the same proof as assertions on the flow chart.

Termination. The pair (u,v)(u, v) goes through the same values as the pair (m,n)(m, n) of Algorithm 1.1E on the same input. At each pass that does not return, vv is replaced by rr with 0<r<v0 < r < v, so vv is a positive integer that gets smaller at each pass, and the algorithm stops after at most nn passes.

Cost. Let kk be the number of passes. It is the number of divisions of Algorithm 1.1E on (m,n)(m, n), so k≤2lg⁡n+2k \le 2 \lg n + 2 when m≥nm \ge n, by the cost bound of Section 1.1 in C1. On (99,78)(99, 78), k=5k = 5, and the bound is 2lg⁡78+2≈14.62 \lg 78 + 2 \approx 14.6. Each pass has one division with remainder and one test. Each pass that does not return also has two multiplications and two subtractions for the coefficients. So the extended algorithm costs a constant factor more than Algorithm 1.1E and has the same order of growth. Section 4.5.2 of the book studies it again.

The exercises

The table has the exercises of Section 1.2.1. The kind is the class of spec 06.3 that C1 proposes, and the indexer of S0 fixes it in the map. The marker ▶ is the book's mark of a recommended exercise.

Exercise Rating Markers Kind Lean Solution
1.2.1-1 05 informal TAOCP.S1_2_1.Ex1.solved X1, 1.2.1-1
1.2.1-2 15 ▶ informal TAOCP.S1_2_1.Ex2.solved X1, 1.2.1-2
1.2.1-3 18 informal TAOCP.S1_2_1.Ex3.solved X1, 1.2.1-3
1.2.1-4 20 K1 TAOCP.S1_2_1.Ex4.solved X1, 1.2.1-4
1.2.1-5 21 K1 TAOCP.S1_2_1.Ex5.solved X1, 1.2.1-5
1.2.1-6 20 K1 TAOCP.S1_2_1.Ex6.solved X1, 1.2.1-6
1.2.1-7 23 K3 TAOCP.S1_2_1.Ex7.solved X1, 1.2.1-7
1.2.1-8 25 ▶ K1 TAOCP.S1_2_1.Ex8.a.solved, TAOCP.S1_2_1.Ex8.b.solved X1, 1.2.1-8
1.2.1-9 20 K1 TAOCP.S1_2_1.Ex9.solved X1, 1.2.1-9
1.2.1-10 22 M K1 TAOCP.S1_2_1.Ex10.solved X1, 1.2.1-10
1.2.1-11 30 M K3 TAOCP.S1_2_1.Ex11.solved X1, 1.2.1-11
1.2.1-12 25 M K5 TAOCP.S1_2_1.Ex12.solved X1, 1.2.1-12
1.2.1-13 23 ▶ M K1 TAOCP.S1_2_1.Ex13.solved X1, 1.2.1-13
1.2.1-14 50 K9 not yet stated X1, 1.2.1-14
1.2.1-15 28 ▶ HM K1 TAOCP.S1_2_1.Ex15.a.statement, TAOCP.S1_2_1.Ex15.b.statement, TAOCP.S1_2_1.Ex15.c.statement, TAOCP.S1_2_1.Ex15.d.statement, TAOCP.S1_2_1.Ex15.e.statement, TAOCP.S1_2_1.Ex15.f.statement, TAOCP.S1_2_1.Ex15.g.statement X1, 1.2.1-15

The order to do them: 1.2.1-1, 2 and 3 first. They are about the form of a proof by induction, and 2 and 3 ask where a wrong proof fails. Then 1.2.1-9 and 10, which are short inductions on inequalities, and 1.2.1-4, the lower bound that goes with eq. 1.2.1(3). Exercise 1.2.1-6 is the step of the proof of eq. 1.2.1(6) in this chapter, in the notation of the book. Exercises 1.2.1-12 and 13 go with the example of a run that does not stop: 12 is the case of numbers u+v2u + v\sqrt2, and 13 adds a clock to the assertions so that they also prove the stop. Exercise 1.2.1-15 studies well-ordered sets in general and extends the last theorem of this chapter. Exercise 1.2.1-14 has the rating 50 and is a research project.

Lean

The Lean code of tamnd/taocp-lean at main (commit b48ef97) has the file TAOCP/V1/S1_2_1.lean. It has the extended Algorithm 1.2.1E as TAOCP.S1_2_1.algorithmE, with the example run of the book as a checked example, and the two equations of eq. 1.2.1(6) as the predicate TAOCP.S1_2_1.Eq6. That predicate only states the two equations. The file has no theorem that they hold at each pass, so the Node line of eq. 1.2.1(6) says "partial". The other Node lines of this chapter say "not yet stated". The exercises 1.2.1-1 to 13 have solutions in TAOCP/Solutions/S1_2_1/, and the exercise 1.2.1-15 has statements in TAOCP/Problems/S1_2_1/. The Mathlib names that the other statements will use are these. Each name was checked with #check against a local build of Mathlib.

  • The principle of induction: Nat.rec (the simple form, from 00) and Nat.strong_induction_on (the strong form). Both are in the list of spec 05.1 for Section 1.2.1, and TAOCP/Base/Check.lean checks them.
  • The well-ordering: Nat.find, Nat.find_spec and Nat.find_min', and WellFounded for a general well-founded relation. TAOCP.CompMethod.steps of Section 1.1 is Nat.find of the stop.
  • The Fibonacci numbers: Nat.fib, with Nat.fib_add_two for the rule Fn+2=Fn+Fn+1F_{n+2} = F_n + F_{n+1}, and TAOCP.fibInt of the project for negative indices. The golden ratio is Real.goldenRatio, and eq. 1.2.1(5) is Real.goldenRatio_sq.
  • The extended Euclid algorithm: Mathlib's Nat.xgcd, Nat.gcdA and Nat.gcdB, with Nat.gcd_eq_gcd_ab for the equation gcd⁡(m,n)=am+bn\gcd(m, n) = am + bn in the integers. These are a reference for the project's Algorithm 1.2.1E, not the algorithm itself. Mathlib does not say that its coefficients are the ones that Algorithm 1.2.1E returns, and a Lean statement that they agree is not yet stated.

The project states Algorithm 1.2.1E as a TAOCP.CompMethod in step format, which is TAOCP.S1_2_1.algorithmE, as TAOCP.S1_1.E.method is for Algorithm 1.1E. Eq. 1.2.1(6) will be an invariant of its step function. The method of assertions is then a theorem about TAOCP.CompMethod.run: if a property holds at the input and the step function keeps it, it holds at each state of the run. Its proof is the proof of Theorem C1.1.2.1.10 above.

Notes and history

Proofs that are induction in all but name are in the work of Francesco Maurolico (1575), and Blaise Pascal used the method in his treatise on the arithmetic triangle (written in 1654). Pierre de Fermat's method of infinite descent is the well-ordering form: a positive integer solution would give a smaller one, and there is no least one. The name "mathematical induction" is due to A. De Morgan (1838). R. Dedekind (1888) and G. Peano (1889) made induction one of the axioms of the integers.

The method of assertions has a long history. H. H. Goldstine and J. von Neumann (1947) put assertion boxes in their flow diagrams, and A. M. Turing (1949) gave a short proof of a program in the same spirit. P. Naur (1966) and R. W. Floyd (1967) made it a general method, and C. A. R. Hoare (1969) turned Floyd's method into a logic of programs, the base of the logics of program correctness that P7 teaches. Floyd's paper also gave the method of proof of termination by a quantity in a well-ordered set.

The partitions p(n)p(n) are the subject of a famous theory. G. H. Hardy and S. Ramanujan (1918) found the growth of p(n)p(n), and H. Rademacher (1937) found an exact series. The extended form of Euclid's algorithm is old: the kuttaka of the Indian mathematician Aryabhata (about 499) solves the equation ax+by=cax + by = c by a process of the same kind.

The project has no errata of Section 1.2.1 to report.

Section 1.2.1 of TAOCP is the first of the mathematical sections of Chapter 1. It teaches proof by induction, and then it uses induction to prove that an algorithm is correct: first the extended form of Euclid's algorithm, then any algorithm, by the method of assertions on a flow chart. This chapter of C1 explains each item of the section, gives each proof in full, and shows why a proof of correctness and a proof of termination are two different things.

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