AnyLearn
All lessons
Mathintermediate

Proof and Induction: Covering Infinitely Many Cases

Testing checks the cases you thought of; a proof covers all of them at once, including the ones nobody will ever run. This lesson builds direct proof, contradiction and induction as working tools, shows the two ways induction fails, and connects it to the loop invariants that make a program correct rather than merely untested.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 10

What a test cannot do

A function taking two 32-bit integers has 2642^{64} possible inputs, about 1.8×10191.8 \times 10^{19}. At a billion cases per second, exhaustive testing finishes in 585 years. Every test suite ever written samples.

A proof does something categorically different. It establishes a property for every input at once, including inputs that will never be constructed, by arguing about the structure of the problem rather than checking instances.

Key idea: Testing and proof answer different questions. Testing asks "did it work on these?" and is cheap, empirical and always incomplete. Proof asks "must it work on all?" and is expensive, deductive, and complete within its assumptions. Serious systems use both, and knowing which one a claim rests on tells you what its failure would look like.

Full lesson text

All 10 steps on one page, for reading, reference, and search.

Show

1. What a test cannot do

A function taking two 32-bit integers has 2642^{64} possible inputs, about 1.8×10191.8 \times 10^{19}. At a billion cases per second, exhaustive testing finishes in 585 years. Every test suite ever written samples.

A proof does something categorically different. It establishes a property for every input at once, including inputs that will never be constructed, by arguing about the structure of the problem rather than checking instances.

Key idea: Testing and proof answer different questions. Testing asks "did it work on these?" and is cheap, empirical and always incomplete. Proof asks "must it work on all?" and is expensive, deductive, and complete within its assumptions. Serious systems use both, and knowing which one a claim rests on tells you what its failure would look like.

2. What counts as a proof

A proof is a finite chain of statements running from things already accepted to the claim, where every link is a step nobody can dispute.

Definition: An axiom is assumed without argument. A theorem is something derived. A proof is the derivation. "Derived" means each step follows by a rule of logic or a previously established result, with nothing skipped that a determined sceptic could question.

The standard is social as much as formal: a proof convinces a careful reader who is actively trying to break it. That is why proofs are written in prose rather than symbols, and why "it is obvious that" is where errors live.

Three shapes cover most of what you will read: prove it directly, assume the opposite and derive nonsense, or prove it for a base case and show it propagates. The rest of this lesson is those three.

3. Direct proof

Assume the hypothesis, apply definitions, arrive at the conclusion. Most proofs are this, and the craft is in unfolding the definitions at the right moment.

Claim. If nn is odd then n2n^2 is odd.

Proof. nn odd means n=2k+1n = 2k + 1 for some integer kk, by definition. Then

n2=(2k+1)2=4k2+4k+1=2(2k2+2k)+1n^2 = (2k+1)^2 = 4k^2 + 4k + 1 = 2(2k^2 + 2k) + 1

Since 2k2+2k2k^2 + 2k is an integer, n2n^2 has the form 2m+12m + 1 and is therefore odd. \blacksquare

Notice where the work happened: at "means", where the word odd became an equation. Definitions are not preamble to a proof, they are the machinery of it, and a proof that never unfolds one is usually not a proof.

4. Contradiction, and Euclid's primes

To prove a statement, assume it is false and derive something impossible. The assumption must then have been wrong.

Claim. There are infinitely many primes.

Proof. Suppose not: the primes are a finite list p1,p2,,pkp_1, p_2, \dots, p_k. Form

N=p1p2pk+1N = p_1 p_2 \cdots p_k + 1

Dividing NN by any pip_i leaves remainder 1, so no prime on the list divides NN. But every integer above 1 has a prime factor, so NN has one, and it is not on the list. The list was supposed to be complete. Contradiction, so no finite list exists. \blacksquare

Gotcha: NN is not claimed to be prime. That misreading is extremely common. 23571113+1=30031=59×5092 \cdot 3 \cdot 5 \cdot 7 \cdot 11 \cdot 13 + 1 = 30031 = 59 \times 509, which is composite, and the proof still works: 59 is a prime that was missing from the list.

5. Induction: the technique for statements about every n

To prove P(n)P(n) holds for all nn0n \geq n_0, prove two things:

Base case: P(n0)Inductive step: P(k)P(k+1)\textbf{Base case: } P(n_0) \qquad\qquad \textbf{Inductive step: } P(k) \Rightarrow P(k+1)

The base case starts the chain and the inductive step keeps it going, so every nn is reached in finitely many hops.

Claim. 1+2++n=n(n+1)21 + 2 + \cdots + n = \dfrac{n(n+1)}{2}.

Base. n=1n = 1: the left side is 1, the right side is 12/2=11 \cdot 2 / 2 = 1.

Step. Assume it holds for kk. Then

1++k+(k+1)=k(k+1)2+(k+1)=(k+1)(k+2)21 + \cdots + k + (k+1) = \frac{k(k+1)}{2} + (k+1) = \frac{(k+1)(k+2)}{2}

which is the formula at k+1k+1. \blacksquare

The assumption P(k)P(k) is the induction hypothesis. Using it is the whole point, not circular reasoning: you are proving an implication, not asserting P(k)P(k).

6. The two ways induction fails

Both failures produce a proof that reads convincingly.

Missing base case. Take P(n)P(n): "n=n+1n = n + 1". The inductive step is fine, since adding 1 to both sides of k=k+1k = k+1 gives k+1=k+2k+1 = k+2. There is simply no base case, and without one the chain starts nowhere. A step that propagates truth propagates falsehood equally well.

A step that fails at one place. The classic: "all horses are the same colour". Base case, one horse, holds. Step: given k+1k+1 horses, drop the last to get kk of one colour, drop the first to get kk of one colour, and the overlapping horses tie the two groups together. This fails at exactly k=1k = 1, where the two groups of one horse do not overlap, so the chain breaks at the first hop and never reaches 3.

Gotcha: "The inductive step works for large kk" is not enough. It must work for every kk from the base case upward, and the small cases are where it usually does not.

7. Strong induction

Sometimes P(k)P(k) alone is not enough to reach P(k+1)P(k+1), and you need every earlier case. Strong induction assumes P(m)P(m) for all m<km < k and proves P(k)P(k) from that.

Claim. Every integer n>1n > 1 is a product of primes.

Proof. Take n>1n > 1 and assume the claim for every smaller integer above 1. Either nn is prime, and it is a product of one prime, or n=abn = ab with 1<a,b<n1 < a, b < n. Both aa and bb are smaller, so by the hypothesis each is a product of primes, and concatenating those factorisations gives one for nn. \blacksquare

In practice: Strong induction is what recursion on subproblems needs. Merge sort's correctness argument is exactly this shape: assume both halves sort correctly, whatever their sizes, then show merging two sorted lists yields a sorted list. The recursion tree is the induction, running downward.

8. Induction that runs in your code

A loop invariant is a statement true before the loop and preserved by each iteration. Establishing one is a base case plus an inductive step, applied to program state.

def maximum(xs):
    best = xs[0]
    # INVARIANT: best is the maximum of xs[0:i+1]
    for i in range(1, len(xs)):
        if xs[i] > best:
            best = xs[i]
    return best

Initialisation. Before the loop, i=0i = 0 and best is the maximum of a one-element slice. True.

Maintenance. If best is the maximum of xs[0:i], then after comparing against xs[i] it is the maximum of xs[0:i+1]. True.

Termination. The loop exits with ii at the end, so the invariant says best is the maximum of the whole list, which is the postcondition.

Those three steps are a proof of correctness for every input list, not for the ones your tests happened to use.

9. One counterexample beats any number of confirmations

Proof and refutation are asymmetric. A universal claim needs an argument covering everything; refuting it needs one instance.

Predict first

The polynomial n^2 + n + 41 gives 41, 43, 47, 53, 61, 71, 83... Every one is prime. It keeps producing primes for n = 0, 1, 2, ... all the way to 39. Is it a prime-generating formula?

Key idea: Forty passing cases is a test suite. It is evidence, and it is not proof. When a pattern holds for every case you checked, the honest position is that you have not found a counterexample yet.

10. Where proof stops

Proof is complete only within its assumptions, and the assumptions are where real systems fail.

ProvedNot thereby proved
The algorithm is correctThe implementation matches the algorithm
The implementation is correctThe compiler translated it faithfully
The program is correctThe hardware executed it, or the spec was what you wanted

Every published proof of correctness sits on assumptions of this kind, and famous failures usually come from one of them rather than from a flawed argument.

Mechanising the checking is what proof assistants and model checkers do, and that is the subject of the separate Formal Verification course in this catalogue. This lesson gives you the reasoning those tools automate; the reason to learn it by hand first is that a tool will happily verify a specification that says the wrong thing.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. A proposed induction proof has a valid inductive step but no base case. What can it establish?
    • The statement for all n above some threshold
    • Nothing: without a base case the implication chain starts nowhere and propagates falsehood equally well
    • The statement for all even n
    • The statement, provided it holds for at least one value found by testing
  2. In Euclid's proof, N = p1p2...pk + 1. What does the proof claim about N?
    • That N is prime
    • That N is the next prime after pk
    • That N is divisible by no prime on the list, so it has a prime factor outside the list
    • That N is composite
  3. Why does the 'all horses are the same colour' proof fail?
    • The base case of one horse is false
    • It uses strong induction where ordinary induction is required
    • The conclusion contradicts an axiom, so the proof is vacuous
    • The inductive step fails at k = 1, where the two overlapping groups do not actually overlap
  4. n^2 + n + 41 produces primes for n = 0 through 39. What does that establish?
    • Nothing universal: it is 40 confirmations, and the claim fails at n = 40
    • That the polynomial generates primes for all n
    • That the polynomial generates primes for all n below 41
    • That a counterexample must exist above n = 1000
  5. Which trio of checks establishes a loop invariant as a correctness proof?
    • Preconditions, postconditions, and exception safety
    • Base case, inductive step, and counterexample search
    • Initialisation, maintenance, and termination
    • Coverage, assertion, and fuzzing

Related lessons

Math
intermediate

Modular Arithmetic: Doing Maths on a Clock

Wrap the number line into a circle and addition and multiplication survive intact while division mostly does not. This lesson builds congruences, shows why you can reduce early to avoid overflow, works through Euclid's algorithm and modular inverses, and explains how a million-digit exponent becomes twenty multiplications.

10 steps·~15 min
Math
intermediate

Graphs: A Language for Relationships

A graph is two sets and an incidence relation, and that austerity is why the same object models build dependencies, social networks, register allocation and road maps. This lesson covers the structural properties worth knowing, the special families that make hard problems easy, and the line where a small change to a question makes it intractable.

10 steps·~15 min
Math
intermediate

Counting Without Listing

Combinatorics answers how many arrangements exist without producing any of them, which is what makes password strength, hash collisions and search-space size computable at all. This lesson builds the product rule, permutations, combinations, inclusion-exclusion and the pigeonhole principle, then applies them to problems where intuition is reliably wrong.

10 steps·~15 min
Computer Science
advanced

Types as Propositions: Making Wrong Programs Unwritable

The other tradition does not prove a program correct after writing it. It designs a type that only correct programs inhabit, so the compiler's ordinary check is the proof. This lesson builds the correspondence between types and logic, shows what dependent types add, and covers the price the approach charges.

8 steps·~12 min