TExcelTAD
Home › Pre-Calculus, Geometry & Trigonometry › Mathematical Induction

Mathematical Induction

How can you prove that a formula like 1+2+⋯+n=n(n+1)21 + 2 + \cdots + n = \frac{n(n + 1)}{2} holds for every whole number nn, when you can only ever check finitely many? Mathematical induction is the answer: prove a first case, then prove that each case implies the next. This lesson explains the method with worked proofs, shows how to check claims in code first, and connects induction to the loop invariants programmers use to reason about loops and recursion.

The domino idea

Imagine an endless row of dominoes. To know they all fall, you need two facts:

  1. the first domino falls;
  2. whenever one domino falls, it knocks over the next.

Induction is that argument for a statement P(n)P(n) about whole numbers nn:

Step What you prove
Base case P(1)P(1) is true (or P(0)P(0), or wherever the claim starts)
Inductive step for any k≥1k \ge 1: if P(k)P(k) is true, then P(k+1)P(k + 1) is true
Conclusion P(n)P(n) is true for every n≥1n \ge 1

In the inductive step, the assumption "P(k)P(k) is true" is called the inductive hypothesis. You do not prove it; you use it to prove P(k+1)P(k + 1).

A first proof

Claim. For every n≥1n \ge 1, 1+2+⋯+n=n(n+1)21 + 2 + \cdots + n = \frac{n(n + 1)}{2}.

Base case (n=1):left side=1,right side=1⋅22=1.✓Inductive step: assume 1+2+⋯+k=k(k+1)2.(hypothesis)1+2+⋯+k+(k+1)=k(k+1)2+(k+1)(use it)1+2+⋯+k+(k+1)=(k+1)(k2+1)1+2+⋯+k+(k+1)=(k+1)(k+2)2←the formula with n=k+1 ✓\begin{aligned} &\text{Base case } (n = 1){:}\quad \text{left side} = 1, \quad \text{right side} = \frac{1 \cdot 2}{2} = 1. \quad \checkmark \\[1ex] &\text{Inductive step: assume } 1 + 2 + \cdots + k = \frac{k(k + 1)}{2}. && \text{(hypothesis)} \\ &\quad 1 + 2 + \cdots + k + (k + 1) = \frac{k(k + 1)}{2} + (k + 1) && \text{(use it)} \\ &\quad \phantom{1 + 2 + \cdots + k + (k + 1)} = (k + 1)\left(\frac{k}{2} + 1\right) \\ &\quad \phantom{1 + 2 + \cdots + k + (k + 1)} = \frac{(k + 1)(k + 2)}{2} && \leftarrow \text{the formula with } n = k + 1 \ \checkmark \end{aligned}

Both steps hold, so the formula is true for all n≥1n \ge 1. The trick is always the same: write the (k+1)(k + 1) case as the kk case plus something, then use the hypothesis.

Check first, then prove

Before trying a proof, test the claim in code. A test cannot prove a statement about infinitely many nn, but it catches false claims quickly and suggests the pattern. For example, is n2+n+41n^2 + n + 41 always prime? It looks like it for a long time:

python · try it yourself
def is_prime(m):
    if m < 2:
        return False
    d = 2
    while d * d <= m:
        if m % d == 0:
            return False
        d += 1
    return True

first_fail = next(n for n in range(1, 100) if not is_prime(n * n + n + 41))
print("prime for n = 1 ..", first_fail - 1)
print("n =", first_fail, ":", first_fail**2 + first_fail + 41, "=", 41, "*", 41)

# a true claim: 1^3 + 2^3 + ... + n^3 = (n(n+1)/2)^2
print(all(sum(k**3 for k in range(1, n + 1)) == (n * (n + 1) // 2)**2 for n in range(1, 500)))
prime for n = 1 .. 39
n = 40 : 1681 = 41 * 41
True

Thirty-nine successes and then failure: checking examples is evidence, not proof. The sum-of-cubes formula passes 499 checks, and induction (Exercise 3) turns that evidence into certainty.

More kinds of claims

Induction works for inequalities and divisibility too.

Claim. 2n>n22^n > n^2 for every n≥5n \ge 5.

Base case (n=5): 32>25. ✓Step: assume 2k>k2 for some k≥5. Then2k+1=2⋅2k>2k2=k2+k2≥k2+5k>k2+2k+1=(k+1)2(k2≥5k because k≥5, and 5k>2k+1 for k≥1). ✓\begin{aligned} &\text{Base case } (n = 5){:}\ 32 > 25. \ \checkmark \\ &\text{Step: assume } 2^k > k^2 \text{ for some } k \ge 5. \text{ Then} \\ &\quad 2^{k+1} = 2 \cdot 2^k > 2k^2 = k^2 + k^2 \ge k^2 + 5k > k^2 + 2k + 1 = (k + 1)^2 \\ &\quad (k^2 \ge 5k \text{ because } k \ge 5, \text{ and } 5k > 2k + 1 \text{ for } k \ge 1). \ \checkmark \end{aligned}

Note the base case is n=5n = 5: the claim is false for n=2,3,4n = 2, 3, 4 (4=44 = 4, 8<98 < 9, 16=1616 = 16).

Claim. 7n−17^n - 1 is divisible by 6 for every n≥1n \ge 1.

Base case: 7−1=6. ✓Step: assume 7k−1=6m. Then 7k+1−1=7⋅7k−1=7(6m+1)−1=42m+6=6(7m+1). ✓\begin{aligned} &\text{Base case: } 7 - 1 = 6. \ \checkmark \\ &\text{Step: assume } 7^k - 1 = 6m. \text{ Then } 7^{k+1} - 1 = 7 \cdot 7^k - 1 = 7(6m + 1) - 1 = 42m + 6 = 6(7m + 1). \ \checkmark \end{aligned}

Note: Strong induction lets the step use all earlier cases P(1),…,P(k)P(1), \ldots, P(k), not only P(k)P(k). It is handy for recursions like Fibonacci, where each term depends on two before it.

In software: loop invariants

A loop invariant is a statement that is true before the loop starts and stays true after every iteration. Proving it is induction on the number of iterations: the base case is "before the first iteration", and the inductive step is "one more iteration keeps it true". When the loop ends, the invariant plus the exit condition tells you the result is correct.

python · try it yourself
def sum_to(n):
    total, i = 0, 0
    # invariant: total == 0 + 1 + ... + i == i*(i+1)//2
    assert total == i * (i + 1) // 2          # base case
    while i < n:
        i += 1
        total += i
        assert total == i * (i + 1) // 2      # the invariant survives one step
    return total                               # loop ended with i == n

print(sum_to(10), sum_to(1000))

def power(x, n):
    result, base, e = 1, x, n
    # invariant: result * base**e == x**n
    while e > 0:
        if e % 2 == 1:
            result *= base
        base, e = base * base, e // 2
        assert result * base**e == x**n
    return result

print(power(3, 13), 3**13)
55 500500
1594323 1594323

Asserting the invariant at run time, as here, is a cheap way to catch bugs while developing. The same reasoning proves that recursive functions are correct: assume the recursive call works on a smaller input (the hypothesis) and show the function then works on the current input. That is how binary search, merge sort and recursion and backtracking are proved correct. A full treatment of proof techniques belongs in Discrete Math.

Exercises

Exercise 1: Odd numbers

Prove by induction that 1+3+5+⋯+(2n−1)=n21 + 3 + 5 + \cdots + (2n - 1) = n^2.

Answer

True for every n≥1n \ge 1 (base case 1=121 = 1^2, step adds 2k+12k + 1).

Solution
Base: n=1: 1=12. ✓Step: assume 1+3+⋯+(2k−1)=k2. Then1+3+⋯+(2k−1)+(2k+1)=k2+2k+1=(k+1)2. ✓\begin{aligned} &\text{Base: } n = 1{:}\ 1 = 1^2. \ \checkmark \\ &\text{Step: assume } 1 + 3 + \cdots + (2k - 1) = k^2. \text{ Then} \\ &\quad 1 + 3 + \cdots + (2k - 1) + (2k + 1) = k^2 + 2k + 1 = (k + 1)^2. \ \checkmark \end{aligned}

Exercise 2: Find the bug

A "proof" that all horses have the same colour: base n=1n = 1 is true. Step: from a group of k+1k + 1 horses, remove one; the other kk share a colour. Remove a different one; those kk share a colour. The groups overlap, so all k+1k + 1 share a colour. Which step fails?

Answer

The step from k=1k = 1 to k=2k = 2: with two horses the two groups of one do not overlap.

Solution

The inductive step must work for every k≥1k \ge 1. For k+1=2k + 1 = 2, removing one horse leaves {A}\{A\}, removing the other leaves {B}\{B\}. These groups share no horse, so nothing links AA's colour to BB's. One broken domino stops the chain.

Exercise 3: Sum of cubes

Prove that 13+23+⋯+n3=(n(n+1)2)21^3 + 2^3 + \cdots + n^3 = \left(\frac{n(n + 1)}{2}\right)^2.

Answer

True for all n≥1n \ge 1.

Solution
Base: n=1: 1=(1⋅2/2)2=1. ✓Step: assume the formula for k. Then(k(k+1)2)2+(k+1)3=(k+1)2⋅(k24+k+1)(k(k+1)2)2+(k+1)3=(k+1)2⋅k2+4k+44(k(k+1)2)2+(k+1)3=((k+1)(k+2)2)2. ✓\begin{aligned} &\text{Base: } n = 1{:}\ 1 = (1 \cdot 2/2)^2 = 1. \ \checkmark \\ &\text{Step: assume the formula for } k. \text{ Then} \\ &\quad \left(\frac{k(k + 1)}{2}\right)^2 + (k + 1)^3 = (k + 1)^2 \cdot \left(\frac{k^2}{4} + k + 1\right) \\ &\quad \phantom{\left(\frac{k(k + 1)}{2}\right)^2 + (k + 1)^3} = (k + 1)^2 \cdot \frac{k^2 + 4k + 4}{4} \\ &\quad \phantom{\left(\frac{k(k + 1)}{2}\right)^2 + (k + 1)^3} = \left(\frac{(k + 1)(k + 2)}{2}\right)^2. \ \checkmark \end{aligned}

Exercise 4: An invariant in code

Write a loop that computes n!n! and asserts an invariant relating fact and i after each step. Print 12!12!.

Answer

Invariant fact == math.factorial(i); 12!=47900160012! = 479001600.

Solution
python · try it yourself
import math

n, fact, i = 12, 1, 0
while i < n:
    i += 1
    fact *= i
    assert fact == math.factorial(i)
print(fact)
479001600

Test yourself

In the inductive step you assume P(k)P(k) and prove…

  1. P(1)P(1)
  2. P(k+1)P(k + 1)
  3. P(k−1)P(k - 1)
  4. P(n)P(n) for all nn

A program checks a formula for n=1n = 1 to 10610^6 and finds no failures. What do you know?

  1. The formula is proved
  2. The formula is false
  3. It holds for those nn only; a proof is still needed
  4. It holds for all even nn

What is a loop invariant?

  1. A variable that never changes
  2. A statement true before the loop and after every iteration
  3. The loop's exit condition
  4. The number of iterations