Sub topic 4
Why It Works: Induction and Recursive Correctness
This chapter assumes you can read the recursive factorial function from the previous chapter and that you have seen proof by mathematical induction at least once. Tracing a call stack shows that factorial(5) returns 120. It does not show that the function is right for every input. Induction gives you that guarantee, and it follows the same structure as the code.
The Parallel Between Recursion and Induction
A proof by induction on n has two parts:
- Base case: show the claim holds for the smallest input, usually n = 0.
- Inductive step: assume the claim holds for n − 1 (the inductive hypothesis), then show it holds for n.
A recursive function has the same two parts. The base case returns an answer directly. The recursive case trusts a smaller call and builds on its result. When you write a recursive function, you are making an inductive argument whether you state it or not.
Proving Factorial Correct
The claim P(n) is: for every integer n ≥ 0, factorial(n) returns n!, where 0! = 1 and n! = n × (n − 1)! for n ≥ 1.
Base case. When n = 0, the function takes the n == 0 branch and returns 1. Since 0! = 1, P(0) holds.
Inductive step. Let n ≥ 1 and assume P(n − 1): factorial(n - 1) returns (n − 1)!. Because n ≥ 1, the function skips the base case and returns n * factorial(n - 1). By the hypothesis, that equals n × (n − 1)!, which is n! by definition. So P(n) holds.
By induction, P(n) holds for every n ≥ 0.
The proof never traces the whole chain of calls. You check one level and trust the level below it. This is often called the recursive leap of faith, and induction is what justifies it.
Termination Is Part of Correctness
The argument above quietly relies on each call moving toward the base case. Two conditions make this hold:
- Every recursive call uses a strictly smaller argument (n − 1 < n).
- Every chain of smaller arguments eventually reaches a base case.
For non-negative integers, both conditions hold. Negative inputs would break the second one, because n − 1 counts down forever and never reaches 0. That is why the function rejects n < 0 explicitly, so the claim only has to cover inputs the proof can reach.
Common Errors That Break the Proof
| Error | Which part of the proof fails |
|---|---|
| Missing base case | No starting point. The calls never stop. |
| Base case returns the wrong value (for example 0) | P(0) is false, and the error carries up to every n. |
| Recursive call on the same or a larger input | The hypothesis does not apply, and the calls may not terminate. |
| Combining step is wrong (for example `n + factorial(n - 1)`) | The inductive step fails even though the hypothesis holds. |
| Base case misses some inputs (only `n == 1`) | Inputs below the base case, such as 0, are never covered. |
Checking the Claim in Code
A proof covers every input. Tests still catch mistakes in the proof or in how the proof was turned into code. Here the function is compared against the standard library:
import math
def factorial(n):
if n < 0:
raise ValueError("n must be non-negative")
if n == 0:
return 1
return n * factorial(n - 1)
for n in range(0, 20):
assert factorial(n) == math.factorial(n), n
print("factorial matches math.factorial for n = 0..19")Exercise
The function power(x, n) returns 1 when n == 0 and returns x * power(x, n - 1) otherwise. Prove by induction that power(x, n) returns x^n for every integer n >= 0.
Show answer
Base case: for n = 0 the function returns 1, and x^0 = 1. Inductive step: let n >= 1 and assume power(x, n - 1) returns x^(n-1). The function returns x power(x, n - 1) = x x^(n-1) = x^n. The argument n - 1 is smaller and still non-negative, so the calls reach n = 0. By induction, the claim holds for all n >= 0.
Exercise
A student changes the base case of factorial to "if n == 1: return 1". Which part of the induction proof breaks, and what happens when factorial(0) is called?
Show answer
The base case no longer covers n = 0, so P(0) is never established. A call to factorial(0) skips the base case and calls factorial(-1), then factorial(-2), and so on, without ever reaching 1. In Python this ends in a RecursionError, unless the n < 0 check is kept, in which case it raises ValueError.