11.3.3 Proofs, induction, and invariants
This part of Discrete math 1 problem set: sets, counting, proofs, induction, relations, bijections develops the worked mechanisms below.
Concepts from other sections (9)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Loop invariant: a statement that holds before a loop, survives each iteration, and with the exit condition implies the result. Read more: 11.3.4 Relations, functions, and validation
- Direct proof: a proof of that assumes , unpacks the definitions, and reasons forward with known results until it reaches . Read more: Chapter 11: Discrete Math 1
- Pigeonhole principle: inputs cannot produce more than distinct outputs, so for equal sizes injective and surjective coincide. Read more: 11.2 Injective, surjective, bijective: the GPT-2 byte map
- Injective: no two inputs share an output; for a dict, no value appears twice. Read more: 11.2 Injective, surjective, bijective: the GPT-2 byte map
- Base case: an input on which a recursive function returns directly without calling itself; together with an input that strictly shrinks on every call, it guarantees that the recursion terminates. Read more: Chapter 13: Recursion and divide-and-conquer in Programming and Algorithms
- Induction hypothesis: the assumption used to prove . Read more: 11.3.4 Relations, functions, and validation
- Strong induction: induction in which the step assumes for every from the base case up to , not just , before proving ; it fits claims where a case depends on several earlier ones. Read more: Chapter 11: Discrete Math 1
- online softmax: computing the maximum and the normalizer in one read by rescaling the running sum whenever the maximum grows. Read more: 22.4 Softmax in C: three-pass and online two-pass in AI Engineering
- Geometric series: , with partial sum and limit for . Read more: 8.2 Series convergence, the EMA as a geometric series, and bias correction
Learning objectives
- Choose a proof method and connect induction to loop invariants.
Proof techniques
Section titled “Proof techniques”q17. Prove directly: if is an odd integer, then is odd. [proof]
Answer
Claim: if is an odd integer, then is odd.
Method: direct proof from the definition (an integer is odd when for some integer ).
Let be odd, so for an integer . Then . Since is an integer, is an integer, and .
By the definition of odd, is odd.
Model proof also in q17.md.
q18. Prove by contrapositive: if is an integer and is even, then is even. [proof]
Answer
Claim: for an integer , if is even then is even.
Method: contrapositive. The statement is logically equivalent to (they have the same truth table), so it suffices to prove: if is not even, then is not even. Every integer is even or odd and not both, so this reads: if is odd, then is odd.
That is exactly q17: write , then is odd.
The contrapositive holds, so the original claim holds: if is even, then is even.
Model proof also in q18.md.
q19. Prove by contradiction: is irrational. [proof]
Answer
Claim: is irrational (it is not for integers with ).
Method: contradiction.
Suppose with integers , . Dividing by common factors, we may assume the fraction is in lowest terms: and share no factor greater than 1.
Squaring gives , so is even, and by q18 is even: for an integer . Substituting, , so is even, and by q18 again is even.
Then 2 divides both and , contradicting lowest terms. So the assumption is false, and is irrational.
Model proof also in q19.md.
q20. Prove that there are infinitely many primes. [proof]
Answer
Claim: there are infinitely many primes (integers whose only positive divisors are 1 and ).
Method: contradiction (Euclid).
Suppose there are only finitely many primes, . Let . Then .
For each , dividing by leaves remainder 1, because divides the product . So no divides .
But every integer greater than 1 has at least one prime factor (q28; or take its smallest divisor greater than 1, which must be prime). That prime factor is a prime not in the list , contradicting that the list holds every prime.
So there are infinitely many primes.
Model proof also in q20.md.
q21. Prove by cases: is even for every integer . [proof]
Answer
Claim: for every integer , is even.
Method: proof by cases. Every integer is even or odd, so the two cases cover all .
Case 1, even: for an integer . Then , and is an integer.
Case 2, odd: . Then , and is an integer.
In both cases is 2 times an integer, so it is even for every integer .
Model proof also in q21.md.
q22. Prove: for real , , with equality exactly when . [proof]
Answer
Claim: for real numbers , , with equality exactly when .
Method: direct, by rewriting the difference as a square.
Because , the real square roots exist, , , and . Then A square of a real number is , so the difference is , which is the inequality.
Equality holds exactly when , that is . Squaring both sides (and taking roots back, both nonnegative) shows this is the same as .
So , with equality if and only if .
Model proof also in q22.md.
q23. Claim: is prime for every integer . (a) Is the claim true? [bool] (b) Give the smallest for which is not prime. [number]
Answer
(a) False, (b) . At : , which is not prime. For the values are prime, which is why checking many cases is not a proof; one counterexample disproves a “for every” claim. A common mistake is 40, which belongs to the sibling polynomial (); here is prime.
q24. Prove (pigeonhole): any function from a set of 257 byte strings to 8-bit hash values maps two different strings to the same value. [proof]
Answer
Claim: any function from a set of 257 byte strings to 8-bit values maps two different strings to the same value.
Method: pigeonhole principle (if more than objects go into boxes, some box gets two).
An 8-bit value is one of possibilities, so the codomain has 256 elements (the boxes). Suppose, for contradiction, that maps different strings to different values, that is, is injective. Then the 257 strings would have 257 distinct images, a subset of size 257 of a set of size 256, which is impossible.
So is not injective: there are strings in with . Every hash table into a fixed number of buckets has collisions once there are more keys than buckets.
Model proof also in q24.md.
q25. Prove: the sum of a rational number and an irrational number is irrational. [proof]
Answer
Claim: if is rational and is irrational, then is irrational.
Method: contradiction.
Suppose for a rational . Then . Write and with integers and . Then with , a ratio of integers, so is rational.
This contradicts the assumption that is irrational. So is not rational: it is irrational.
Model proof also in q25.md.
Induction and loop invariants
Section titled “Induction and loop invariants”q26. Prove by induction: for every integer . [proof]
Answer
Claim: for every integer .
Method: induction on .
Base case : the left side is and the right side is .
Induction hypothesis: for some integer , .
Inductive step: by the hypothesis, , which is the formula for .
By induction, the formula holds for every .
Model proof also in q26.md.
q27. Prove by induction: for every integer . [proof]
Answer
Claim: for every integer .
Method: induction on starting at 5.
Base case : .
Induction hypothesis: for some integer .
Inductive step: by the hypothesis. Now is the same as , that is , that is , which holds for every and so for . Therefore .
By induction, for every . (The claim fails at , where , which is why the base case is 5.)
Model proof also in q27.md.
q28. Prove by strong induction: every integer is a product of one or more primes. [proof]
Answer
Claim: every integer is a product of one or more primes.
Method: strong induction on .
Strong hypothesis: for a fixed , every integer with is a product of primes. We show is too.
Case 1, is prime: then itself is a product of one prime. This covers the base case , which is prime.
Case 2, is not prime: then has a divisor with , so with , and as well. Both and lie in , so by the hypothesis and are products of primes, and is one too.
By strong induction, every integer is a product of primes.
Model proof also in q28.md.
q29. Prove that this loop returns for every integer , using the invariant (the same squaring trick computes PCG32’s jump-ahead):
r = 1; b = x; e = nwhile e > 0: if e is odd: r = r * b b = b * b e = floor(e / 2)return r[proof]
Answer
Claim: for every integer the loop returns .
Method: loop invariant : , with a nonnegative integer.
Initialization: before the first iteration , , , so .
Maintenance: suppose holds and at the top of an iteration; write for the values after it.
- If is odd, with . Then , , , and .
- If is even, . Then , , , and . In both cases holds again and is a nonnegative integer.
Termination: while , , so is a strictly decreasing sequence of nonnegative integers and reaches 0 after finitely many iterations.
At exit , so gives , and the loop returns .
Model proof also in q29.md.
q30. The online softmax (the C kernel of Softmax in C: three-pass and online two-pass) reads once, keeping a running maximum and a running sum . It starts at , (with ), and for each new sets and . Prove that after reading it holds that and . [proof]
Answer
Claim: after the loop has read , and (for : , the max of nothing, and , the empty sum).
Method: induction on , the number of elements read (a loop invariant).
Base : the loop starts with and , which are the empty max and the empty sum.
Step: assume the claim after elements, with values , and read . Then . For the sum, using the hypothesis and : so . When , and the first term is 0 (with ), so , which also matches. The claim holds after elements.
By induction it holds after every element. Every exponent is , so no term exceeds 1 and never overflows.
Model proof also in q30.md.
q31. Give a closed form for . [expr in n]
Answer
. Provable by induction like q26: the step adds and . Check: gives . Common mistakes: is only the leading term, and is the sum of , not .
q32. Give a closed form for . [expr in n]
Answer
. A geometric series: . Check: gives . A common mistake is , an off-by-one in the last term (that sums up to ).
q33. Run the loop of q30 on . Give the final exactly. [number]
Answer
. Start , . Read 1: , . Read 3: , . Read 2: , . This equals , as q30 promises. Common mistakes: forgets the shift by the running max, which is exactly what keeps from overflowing; drops the max element’s own term .
Review questions
Section titled “Review questions”-
What must be established for an invariant to prove a loop result?
Answer
Initialization, preservation, and the postcondition on termination; termination itself requires a separate argument when claiming total correctness.