Skip to content

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.

Learning objectives

q17. Prove directly: if nn is an odd integer, then n2n^2 is odd. [proof]

Answer

Claim: if nn is an odd integer, then n2n^2 is odd.

Method: direct proof from the definition (an integer nn is odd when n=2k+1n = 2k + 1 for some integer kk).

Let nn be odd, so n=2k+1n = 2k + 1 for an integer kk. Then n2=4k2+4k+1=2(2k2+2k)+1n^2 = 4k^2 + 4k + 1 = 2(2k^2 + 2k) + 1. Since kk is an integer, j=2k2+2kj = 2k^2 + 2k is an integer, and n2=2j+1n^2 = 2j + 1.

By the definition of odd, n2n^2 is odd.

Model proof also in q17.md.

q18. Prove by contrapositive: if nn is an integer and n2n^2 is even, then nn is even. [proof]

Answer

Claim: for an integer nn, if n2n^2 is even then nn is even.

Method: contrapositive. The statement P⇒QP \Rightarrow Q is logically equivalent to ¬Q⇒¬P\lnot Q \Rightarrow \lnot P (they have the same truth table), so it suffices to prove: if nn is not even, then n2n^2 is not even. Every integer is even or odd and not both, so this reads: if nn is odd, then n2n^2 is odd.

That is exactly q17: write n=2k+1n = 2k + 1, then n2=2(2k2+2k)+1n^2 = 2(2k^2 + 2k) + 1 is odd.

The contrapositive holds, so the original claim holds: if n2n^2 is even, then nn is even.

Model proof also in q18.md.

q19. Prove by contradiction: 2\sqrt{2} is irrational. [proof]

Answer

Claim: 2\sqrt{2} is irrational (it is not p/qp/q for integers p,qp, q with q≠0q \ne 0).

Method: contradiction.

Suppose 2=p/q\sqrt{2} = p/q with integers p,qp, q, q≠0q \ne 0. Dividing by common factors, we may assume the fraction is in lowest terms: pp and qq share no factor greater than 1.

Squaring gives p2=2q2p^2 = 2q^2, so p2p^2 is even, and by q18 pp is even: p=2rp = 2r for an integer rr. Substituting, 4r2=2q24r^2 = 2q^2, so q2=2r2q^2 = 2r^2 is even, and by q18 again qq is even.

Then 2 divides both pp and qq, contradicting lowest terms. So the assumption is false, and 2\sqrt{2} 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 p≥2p \ge 2 whose only positive divisors are 1 and pp).

Method: contradiction (Euclid).

Suppose there are only finitely many primes, p1,p2,…,pkp_1, p_2, \dots, p_k. Let N=p1p2⋯pk+1N = p_1 p_2 \cdots p_k + 1. Then N≥3>1N \ge 3 > 1.

For each ii, dividing NN by pip_i leaves remainder 1, because pip_i divides the product p1⋯pkp_1 \cdots p_k. So no pip_i divides NN.

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 p1,…,pkp_1, \dots, p_k, contradicting that the list holds every prime.

So there are infinitely many primes.

Model proof also in q20.md.

q21. Prove by cases: n2+nn^2 + n is even for every integer nn. [proof]

Answer

Claim: for every integer nn, n2+nn^2 + n is even.

Method: proof by cases. Every integer is even or odd, so the two cases cover all nn.

Case 1, nn even: n=2kn = 2k for an integer kk. Then n2+n=4k2+2k=2(2k2+k)n^2 + n = 4k^2 + 2k = 2(2k^2 + k), and 2k2+k2k^2 + k is an integer.

Case 2, nn odd: n=2k+1n = 2k + 1. Then n2+n=4k2+4k+1+2k+1=4k2+6k+2=2(2k2+3k+1)n^2 + n = 4k^2 + 4k + 1 + 2k + 1 = 4k^2 + 6k + 2 = 2(2k^2 + 3k + 1), and 2k2+3k+12k^2 + 3k + 1 is an integer.

In both cases n2+nn^2 + n is 2 times an integer, so it is even for every integer nn.

Model proof also in q21.md.

q22. Prove: for real a,b≥0a, b \ge 0, a+b2≥ab\frac{a + b}{2} \ge \sqrt{ab}, with equality exactly when a=ba = b. [proof]

Answer

Claim: for real numbers a,b≥0a, b \ge 0, a+b2≥ab\frac{a+b}{2} \ge \sqrt{ab}, with equality exactly when a=ba = b.

Method: direct, by rewriting the difference as a square.

Because a,b≥0a, b \ge 0, the real square roots a,b\sqrt{a}, \sqrt{b} exist, a=(a)2a = (\sqrt{a})^2, b=(b)2b = (\sqrt{b})^2, and ab=ab\sqrt{ab} = \sqrt{a}\sqrt{b}. Then a+b2−ab=(a)2−2ab+(b)22=(a−b)22.\frac{a + b}{2} - \sqrt{ab} = \frac{(\sqrt a)^2 - 2\sqrt a \sqrt b + (\sqrt b)^2}{2} = \frac{(\sqrt a - \sqrt b)^2}{2}. A square of a real number is ≥0\ge 0, so the difference is ≥0\ge 0, which is the inequality.

Equality holds exactly when (a−b)2=0(\sqrt a - \sqrt b)^2 = 0, that is a=b\sqrt a = \sqrt b. Squaring both sides (and taking roots back, both nonnegative) shows this is the same as a=ba = b.

So a+b2≥ab\frac{a+b}{2} \ge \sqrt{ab}, with equality if and only if a=ba = b.

Model proof also in q22.md.

q23. Claim: n2−n+41n^2 - n + 41 is prime for every integer n≥1n \ge 1. (a) Is the claim true? [bool] (b) Give the smallest n≥1n \ge 1 for which n2−n+41n^2 - n + 41 is not prime. [number]

Answer

(a) False, (b) 4141. At n=41n = 41: 412−41+41=41241^2 - 41 + 41 = 41^2, which is not prime. For n=1,…,40n = 1, \dots, 40 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 n2+n+41n^2 + n + 41 (402+40+41=41240^2 + 40 + 41 = 41^2); here 402−40+41=160140^2 - 40 + 41 = 1601 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 hh from a set SS of 257 byte strings to 8-bit values maps two different strings to the same value.

Method: pigeonhole principle (if more than kk objects go into kk boxes, some box gets two).

An 8-bit value is one of 28=2562^8 = 256 possibilities, so the codomain has 256 elements (the boxes). Suppose, for contradiction, that hh maps different strings to different values, that is, hh 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 hh is not injective: there are strings s≠ts \ne t in SS with h(s)=h(t)h(s) = h(t). 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 rr is rational and xx is irrational, then r+xr + x is irrational.

Method: contradiction.

Suppose r+x=qr + x = q for a rational qq. Then x=q−rx = q - r. Write q=a/bq = a/b and r=c/dr = c/d with integers a,b,c,da, b, c, d and b,d≠0b, d \ne 0. Then x=(ad−bc)/(bd)x = (ad - bc)/(bd) with bd≠0bd \ne 0, a ratio of integers, so xx is rational.

This contradicts the assumption that xx is irrational. So r+xr + x is not rational: it is irrational.

Model proof also in q25.md.

q26. Prove by induction: 1+2+⋯+n=n(n+1)21 + 2 + \dots + n = \frac{n(n+1)}{2} for every integer n≥1n \ge 1. [proof]

Answer

Claim: ∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2} for every integer n≥1n \ge 1.

Method: induction on nn.

Base case n=1n = 1: the left side is 11 and the right side is 1⋅2/2=11 \cdot 2 / 2 = 1.

Induction hypothesis: for some integer k≥1k \ge 1, ∑i=1ki=k(k+1)2\sum_{i=1}^{k} i = \frac{k(k+1)}{2}.

Inductive step: ∑i=1k+1i=(∑i=1ki)+(k+1)=k(k+1)2+(k+1)\sum_{i=1}^{k+1} i = \left(\sum_{i=1}^{k} i\right) + (k + 1) = \frac{k(k+1)}{2} + (k+1) by the hypothesis, =k(k+1)+2(k+1)2=(k+1)(k+2)2= \frac{k(k+1) + 2(k+1)}{2} = \frac{(k+1)(k+2)}{2}, which is the formula for n=k+1n = k + 1.

By induction, the formula holds for every n≥1n \ge 1.

Model proof also in q26.md.

q27. Prove by induction: 2n>n22^n > n^2 for every integer n≥5n \ge 5. [proof]

Answer

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

Method: induction on nn starting at 5.

Base case n=5n = 5: 25=32>25=522^5 = 32 > 25 = 5^2.

Induction hypothesis: 2k>k22^k > k^2 for some integer k≥5k \ge 5.

Inductive step: 2k+1=2⋅2k>2k22^{k+1} = 2 \cdot 2^k > 2k^2 by the hypothesis. Now 2k2≥(k+1)22k^2 \ge (k+1)^2 is the same as k2≥2k+1k^2 \ge 2k + 1, that is k2−2k−1≥0k^2 - 2k - 1 \ge 0, that is (k−1)2≥2(k - 1)^2 \ge 2, which holds for every k≥3k \ge 3 and so for k≥5k \ge 5. Therefore 2k+1>2k2≥(k+1)22^{k+1} > 2k^2 \ge (k+1)^2.

By induction, 2n>n22^n > n^2 for every n≥5n \ge 5. (The claim fails at n=4n = 4, where 16=1616 = 16, which is why the base case is 5.)

Model proof also in q27.md.

q28. Prove by strong induction: every integer n≥2n \ge 2 is a product of one or more primes. [proof]

Answer

Claim: every integer n≥2n \ge 2 is a product of one or more primes.

Method: strong induction on nn.

Strong hypothesis: for a fixed n≥2n \ge 2, every integer mm with 2≤m<n2 \le m < n is a product of primes. We show nn is too.

Case 1, nn is prime: then nn itself is a product of one prime. This covers the base case n=2n = 2, which is prime.

Case 2, nn is not prime: then nn has a divisor aa with 1<a<n1 < a < n, so n=abn = ab with b=n/ab = n / a, and 1<b<n1 < b < n as well. Both aa and bb lie in [2,n)[2, n), so by the hypothesis a=p1⋯pra = p_1 \cdots p_r and b=q1⋯qsb = q_1 \cdots q_s are products of primes, and n=p1⋯prq1⋯qsn = p_1 \cdots p_r q_1 \cdots q_s is one too.

By strong induction, every integer n≥2n \ge 2 is a product of primes.

Model proof also in q28.md.

q29. Prove that this loop returns xnx^n for every integer n≥0n \ge 0, using the invariant r⋅be=xnr \cdot b^e = x^n (the same squaring trick computes PCG32’s jump-ahead):

r = 1; b = x; e = n
while e > 0:
if e is odd: r = r * b
b = b * b
e = floor(e / 2)
return r

[proof]

Answer

Claim: for every integer n≥0n \ge 0 the loop returns xnx^n.

Method: loop invariant II: r⋅be=xnr \cdot b^e = x^n, with ee a nonnegative integer.

Initialization: before the first iteration r=1r = 1, b=xb = x, e=ne = n, so r⋅be=xnr \cdot b^e = x^n.

Maintenance: suppose II holds and e>0e > 0 at the top of an iteration; write r′,b′,e′r', b', e' for the values after it.

  • If ee is odd, e=2t+1e = 2t + 1 with t=⌊e/2⌋t = \lfloor e/2 \rfloor. Then r′=rbr' = rb, b′=b2b' = b^2, e′=te' = t, and r′b′e′=rb⋅b2t=rb2t+1=rbe=xnr' b'^{e'} = r b \cdot b^{2t} = r b^{2t+1} = r b^e = x^n.
  • If ee is even, e=2te = 2t. Then r′=rr' = r, b′=b2b' = b^2, e′=te' = t, and r′b′e′=rb2t=rbe=xnr' b'^{e'} = r b^{2t} = r b^e = x^n. In both cases II holds again and e′e' is a nonnegative integer.

Termination: while e>0e > 0, e′=⌊e/2⌋<ee' = \lfloor e/2 \rfloor < e, so ee is a strictly decreasing sequence of nonnegative integers and reaches 0 after finitely many iterations.

At exit e=0e = 0, so II gives r⋅b0=r=xnr \cdot b^0 = r = x^n, and the loop returns xnx^n.

Model proof also in q29.md.

q30. The online softmax (the C kernel of Softmax in C: three-pass and online two-pass) reads x0,x1,…x_0, x_1, \dots once, keeping a running maximum mm and a running sum ss. It starts at m=−∞m = -\infty, s=0s = 0 (with e−∞=0e^{-\infty} = 0), and for each new xix_i sets m′=max⁡(m,xi)m' = \max(m, x_i) and s′=s⋅em−m′+exi−m′s' = s \cdot e^{m - m'} + e^{x_i - m'}. Prove that after reading x0,…,xi−1x_0, \dots, x_{i-1} it holds that m=max⁡j<ixjm = \max_{j < i} x_j and s=∑j<iexj−ms = \sum_{j < i} e^{x_j - m}. [proof]

Answer

Claim: after the loop has read x0,…,xi−1x_0, \dots, x_{i-1}, m=max⁡j<ixjm = \max_{j < i} x_j and s=∑j<iexj−ms = \sum_{j<i} e^{x_j - m} (for i=0i = 0: m=−∞m = -\infty, the max of nothing, and s=0s = 0, the empty sum).

Method: induction on ii, the number of elements read (a loop invariant).

Base i=0i = 0: the loop starts with m=−∞m = -\infty and s=0s = 0, which are the empty max and the empty sum.

Step: assume the claim after ii elements, with values m,sm, s, and read xix_i. Then m′=max⁡(m,xi)=max⁡(max⁡j<ixj,xi)=max⁡j≤ixjm' = \max(m, x_i) = \max(\max_{j<i} x_j, x_i) = \max_{j \le i} x_j. For the sum, using the hypothesis and eaeb=ea+be^{a}e^{b} = e^{a+b}: s⋅em−m′=∑j<iexj−mem−m′=∑j<iexj−m′,s \cdot e^{m - m'} = \sum_{j<i} e^{x_j - m} e^{m - m'} = \sum_{j<i} e^{x_j - m'}, so s′=sem−m′+exi−m′=∑j≤iexj−m′s' = s e^{m - m'} + e^{x_i - m'} = \sum_{j \le i} e^{x_j - m'}. When i=0i = 0, s=0s = 0 and the first term is 0 (with e−∞=0e^{-\infty} = 0), so s′=ex0−x0=1s' = e^{x_0 - x_0} = 1, which also matches. The claim holds after i+1i + 1 elements.

By induction it holds after every element. Every exponent xj−mx_j - m is ≤0\le 0, so no term exceeds 1 and ss never overflows.

Model proof also in q30.md.

q31. Give a closed form for 12+22+⋯+n21^2 + 2^2 + \dots + n^2. [expr in n]

Answer

n(n+1)(2n+1)6\frac{n(n + 1)(2n + 1)}{6}. Provable by induction like q26: the step adds (k+1)2(k + 1)^2 and k(k+1)(2k+1)6+(k+1)2=(k+1)(k+2)(2k+3)6\frac{k(k+1)(2k+1)}{6} + (k+1)^2 = \frac{(k+1)(k+2)(2k+3)}{6}. Check: n=2n = 2 gives 55. Common mistakes: n3/3n^3/3 is only the leading term, and n(n+1)2\frac{n(n+1)}{2} is the sum of nn, not n2n^2.

q32. Give a closed form for 20+21+⋯+2n−12^0 + 2^1 + \dots + 2^{n-1}. [expr in n]

Answer

2n−12^n - 1. A geometric series: ∑i=0n−12i=2n−12−1\sum_{i=0}^{n-1} 2^i = \frac{2^n - 1}{2 - 1}. Check: n=3n = 3 gives 1+2+4=71 + 2 + 4 = 7. A common mistake is 2n+1−12^{n+1} - 1, an off-by-one in the last term (that sums up to 2n2^n).

q33. Run the loop of q30 on x=(1,3,2)x = (1, 3, 2). Give the final ss exactly. [number]

Answer

1+e−1+e−21 + e^{-1} + e^{-2}. Start m=−∞m = -\infty, s=0s = 0. Read 1: m=1m = 1, s=0+e0=1s = 0 + e^0 = 1. Read 3: m=3m = 3, s=1⋅e1−3+e0=e−2+1s = 1 \cdot e^{1 - 3} + e^0 = e^{-2} + 1. Read 2: m=3m = 3, s=(1+e−2)⋅e0+e2−3=1+e−1+e−2s = (1 + e^{-2}) \cdot e^0 + e^{2 - 3} = 1 + e^{-1} + e^{-2}. This equals ∑jexj−3\sum_j e^{x_j - 3}, as q30 promises. Common mistakes: e1+e3+e2e^1 + e^3 + e^2 forgets the shift by the running max, which is exactly what keeps ss from overflowing; e−1+e−2e^{-1} + e^{-2} drops the max element’s own term e0=1e^0 = 1.

  1. 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.

Contents · Next: Relations, functions, and validation