TExcelTAD
Home › Discrete Mathematics › Ordinary Induction

Ordinary Induction

Mathematical induction proves a statement P(n)P(n) for every natural number nn in two steps. It is the most important proof technique in computer science, because it is the logic of loops and recursion: if the first step works and each step preserves correctness, every step works.

The principle

To prove ∀n∈N. P(n)\forall n \in \mathbb{N}.\ P(n):

  1. Base case: prove P(0)P(0).
  2. Inductive step: prove P(n)→P(n+1)P(n) \to P(n + 1) for every n≥0n \ge 0. The assumption P(n)P(n) is the induction hypothesis.

Think of dominoes: the first falls, and each falling domino knocks over the next, so all fall. The principle is equivalent to the Well Ordering Principle, but usually shorter to write.

A sum formula

Theorem. For all n≥0n \ge 0: ∑i=0n2i=2n+1−1\displaystyle\sum_{i=0}^{n} 2^i = 2^{n+1} - 1.

Proof by induction on nn, with P(n)P(n) the formula.

Base case n=0n = 0: the left side is 20=12^0 = 1, the right side is 21−1=12^1 - 1 = 1.

Inductive step: assume P(n)P(n). Then

∑i=0n+12i=(∑i=0n2i)+2n+1split off the last term=(2n+1−1)+2n+1induction hypothesis=2n+2−1\begin{aligned} \sum_{i=0}^{n+1} 2^i &= \left(\sum_{i=0}^{n} 2^i\right) + 2^{n+1} && \text{split off the last term} \\ &= (2^{n+1} - 1) + 2^{n+1} && \text{induction hypothesis} \\ &= 2^{n+2} - 1 \end{aligned}

which is P(n+1)P(n + 1). ■\blacksquare

This is why the largest unsigned 32-bit integer is 232−12^{32} - 1: it is the sum of 32 one-bits.

python · try it yourself
for n in range(8):
    assert sum(2 ** i for i in range(n + 1)) == 2 ** (n + 1) - 1
print("checked n = 0..7")
print("sum of 2^i for i = 0..31:", sum(2 ** i for i in range(32)), "= 2^32 - 1 =", 2 ** 32 - 1)
checked n = 0..7
sum of 2^i for i = 0..31: 4294967295 = 2^32 - 1 = 4294967295

An inequality and a divisibility claim

The base case does not have to be 0. To prove P(n)P(n) for all n≥bn \ge b, prove P(b)P(b) and P(n)→P(n+1)P(n) \to P(n + 1) for n≥bn \ge b.

Theorem. 2n>n22^n > n^2 for all n≥5n \ge 5.

Proof. Base: 25=32>252^5 = 32 > 25. Step: assume 2n>n22^n > n^2 with n≥5n \ge 5. Then 2n+1=2⋅2n>2n22^{n+1} = 2 \cdot 2^n > 2n^2. It remains to show 2n2≥(n+1)22n^2 \ge (n + 1)^2, that is n2≥2n+1n^2 \ge 2n + 1, which holds because n2≥5n=2n+3n>2n+1n^2 \ge 5n = 2n + 3n > 2n + 1 for n≥5n \ge 5. So 2n+1>(n+1)22^{n+1} > (n + 1)^2. ■\blacksquare

Theorem. 3∣n3−n3 \mid n^3 - n for all n≥0n \ge 0.

Proof. Base: 03−0=00^3 - 0 = 0. Step: (n+1)3−(n+1)=n3+3n2+3n+1−n−1=(n3−n)+3(n2+n)(n + 1)^3 - (n + 1) = n^3 + 3n^2 + 3n + 1 - n - 1 = (n^3 - n) + 3(n^2 + n). The first term is divisible by 3 by the hypothesis, the second obviously. ■\blacksquare

A tiling argument

Induction also proves the existence of constructions. A 2n×2n2^n \times 2^n courtyard with one square removed (for a statue) can always be tiled with L-shaped trominoes (three squares). Induction: split the board into four 2n−1×2n−12^{n-1} \times 2^{n-1} quarters; the statue lies in one of them; place one L-tromino at the centre covering one square of each of the other three quarters; now each quarter has exactly one square that is missing or covered, and the hypothesis tiles each. The proof is a recursive algorithm:

python · try it yourself
def tile(n, hole):
    size = 2 ** n
    board = [[0] * size for _ in range(size)]
    counter = [0]

    def solve(top, left, s, hr, hc):
        if s == 1:
            return
        counter[0] += 1
        t, h = counter[0], s // 2
        holes = []
        for dr in (0, 1):
            for dc in (0, 1):
                r0, c0 = top + dr * h, left + dc * h
                if r0 <= hr < r0 + h and c0 <= hc < c0 + h:
                    holes.append((r0, c0, hr, hc))
                else:                                   # cover the corner touching the centre
                    cr, cc = top + h - 1 + dr, left + h - 1 + dc
                    board[cr][cc] = t
                    holes.append((r0, c0, cr, cc))
        for r0, c0, a, b in holes:
            solve(r0, c0, h, a, b)

    board[hole[0]][hole[1]] = -1
    solve(0, 0, size, *hole)
    return board, counter[0]

board, used = tile(3, (2, 5))
for row in board:
    print(" ".join(" #" if v == -1 else f"{v:2d}" for v in row))
print("trominoes used:", used, "= (4^3 - 1) / 3 =", (4 ** 3 - 1) // 3)
 3  3  4  4  8  8  9  9
 3  2  2  4  8  7  7  9
 5  2  6  6 10  #  7 11
 5  5  6  1 10 10 11 11
13 13 14  1  1 18 19 19
13 12 14 14 18 18 17 19
15 12 12 16 20 17 17 21
15 15 16 16 20 20 21 21
trominoes used: 21 = (4^3 - 1) / 3 = 21

Each number marks one L-shaped piece and # is the statue.

Common mistakes

  • Forgetting the base case. "n=n+1n = n + 1 implies n+1=n+2n + 1 = n + 2" is a valid inductive step, but there is no base case and the claim is false.
  • A step that fails for small nn. The famous "all horses are the same color" argument uses two overlapping groups of nn horses; when going from 1 to 2 horses the groups do not overlap, and the proof breaks there.

In software

A recursive function is correct by induction: prove it for the base case, then prove each call is correct assuming the smaller recursive calls are. A loop is correct by induction on the number of iterations, using a loop invariant.

Exercises

Exercise 1: Sum of squares

Prove ∑i=1ni2=n(n+1)(2n+1)6\sum_{i=1}^{n} i^2 = \frac{n(n + 1)(2n + 1)}{6} for n≥1n \ge 1.

Answer

Base n=1n = 1: 1=11 = 1. Step: add (n+1)2(n + 1)^2 and factor.

Solution

Base: 12=1⋅2⋅361^2 = \frac{1 \cdot 2 \cdot 3}{6}. Step: assuming the formula for nn,

n(n+1)(2n+1)6+(n+1)2=(n+1)(n(2n+1)+6(n+1))6=(n+1)(2n2+7n+6)6=(n+1)(n+2)(2n+3)6\begin{aligned} \frac{n(n+1)(2n+1)}{6} + (n+1)^2 &= \frac{(n+1)\left(n(2n+1) + 6(n+1)\right)}{6} \\ &= \frac{(n+1)(2n^2 + 7n + 6)}{6} = \frac{(n+1)(n+2)(2n+3)}{6} \end{aligned}

which is the formula for n+1n + 1. ■\blacksquare

Exercise 2: Divisibility

Prove that 4n−14^n - 1 is divisible by 3 for all n≥0n \ge 0.

Answer

4n+1−1=4(4n−1)+34^{n+1} - 1 = 4(4^n - 1) + 3.

Solution

Base: 40−1=04^0 - 1 = 0. Step: 4n+1−1=4⋅4n−4+3=4(4n−1)+34^{n+1} - 1 = 4 \cdot 4^n - 4 + 3 = 4(4^n - 1) + 3. Both terms are multiples of 3, the first by the hypothesis. ■\blacksquare

Exercise 3: Find the flaw

"Claim: every n≥1n \ge 1 satisfies n=n+1n = n + 1. Step: if n=n+1n = n + 1, add 1 to both sides to get n+1=n+2n + 1 = n + 2." What is wrong?

Answer

The base case fails: 1≠21 \ne 2.

Solution

The inductive step is logically fine, but without a true base case nothing gets started. Induction needs both parts.

Exercise 4: Count tromino tilings' pieces (code)

How many trominoes tile a 2n×2n2^n \times 2^n board with one square removed? Check the formula 4n−13\frac{4^n - 1}{3} for n=1,…,6n = 1, \ldots, 6.

Answer

4n−13\frac{4^n - 1}{3}: 1, 5, 21, 85, 341, 1365.

Solution

The board has 4n−14^n - 1 free squares and each piece covers 3. Exercise 2 shows this is always an integer.

python · try it yourself
print([(4 ** n - 1) // 3 for n in range(1, 7)], all((4 ** n - 1) % 3 == 0 for n in range(1, 7)))
[1, 5, 21, 85, 341, 1365] True

Test yourself

In the inductive step you prove…

  1. P(0)P(0)
  2. P(n)P(n) for all nn directly
  3. P(n)→P(n+1)P(n) \to P(n + 1)
  4. P(n+1)→P(n)P(n + 1) \to P(n)

To prove P(n)P(n) for all n≥5n \ge 5, the base case is…

  1. P(0)P(0)
  2. P(1)P(1)
  3. P(5)P(5)
  4. P(6)P(6)

Which programming construct is proved correct by induction most naturally?

  1. A constant declaration
  2. A recursive function
  3. An import statement
  4. A comment