Mathematical Induction
How can you prove that a formula like holds for every whole number , 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:
- the first domino falls;
- whenever one domino falls, it knocks over the next.
Induction is that argument for a statement about whole numbers :
| Step | What you prove |
|---|---|
| Base case | is true (or , or wherever the claim starts) |
| Inductive step | for any : if is true, then is true |
| Conclusion | is true for every |
In the inductive step, the assumption " is true" is called the inductive hypothesis. You do not prove it; you use it to prove .
A first proof
Claim. For every , .
Both steps hold, so the formula is true for all . The trick is always the same: write the case as the 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 , but it catches false claims quickly and suggests the pattern. For example, is always prime? It looks like it for a long time:
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. for every .
Note the base case is : the claim is false for (, , ).
Claim. is divisible by 6 for every .
Note: Strong induction lets the step use all earlier cases , not only . 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.
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 .
Answer
True for every (base case , step adds ).
Solution
Exercise 2: Find the bug
A "proof" that all horses have the same colour: base is true. Step: from a group of horses, remove one; the other share a colour. Remove a different one; those share a colour. The groups overlap, so all share a colour. Which step fails?
Answer
The step from to : with two horses the two groups of one do not overlap.
Solution
The inductive step must work for every . For , removing one horse leaves , removing the other leaves . These groups share no horse, so nothing links 's colour to 's. One broken domino stops the chain.
Exercise 3: Sum of cubes
Prove that .
Answer
True for all .
Solution
Exercise 4: An invariant in code
Write a loop that computes and asserts an invariant relating fact and i after each step. Print .
Answer
Invariant fact == math.factorial(i); .
Solution
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 and prove…
- for all
A program checks a formula for to and finds no failures. What do you know?
- The formula is proved
- The formula is false
- It holds for those only; a proof is still needed
- It holds for all even
What is a loop invariant?
- A variable that never changes
- A statement true before the loop and after every iteration
- The loop's exit condition
- The number of iterations