CMP-250 · Lesson 5 of 12 · 36 min

Proof techniques I: direct proof and contrapositive

A proof is a sequence of statements, each justified by a definition, an axiom, an earlier result or a valid rule of inference, that ends with the statement to be proved. This lesson teaches the two most common techniques for proving conditional statements: direct proof and proof by contrapositive. It also covers how to disprove a universal claim with a counterexample and how to write proofs that other people can read and check.

The structure of a theorem

Most theorems have the shape \(\forall x\, (P(x) \to Q(x))\): for every object of some kind, if it has property \(P\) then it has property \(Q\). Often the universal quantifier is left implicit. If \(n\) is odd, then \(n^2\) is odd really means for every integer \(n\), if \(n\) is odd then \(n^2\) is odd. To prove a universal statement, you let \(x\) be an arbitrary object of the domain and prove the claim for that \(x\) using only properties every such object has. Because nothing special was assumed about \(x\), the argument works for all of them. This step is called universal generalization.

Proofs in this lesson use a few basic facts about integers: they are closed under addition, subtraction and multiplication; every integer is either even or odd and not both; and the definitions below.

Definitions used in this lesson
TermDefinition
Evenn is even if n = 2k for some integer k
Oddn is odd if n = 2k + 1 for some integer k
Dividesa divides b, written a | b, if b = ak for some integer k (a ≠ 0)
PrimeAn integer p > 1 whose only positive divisors are 1 and p
CompositeAn integer n > 1 that is not prime, so n = ab with 1 < a, b < n
RationalA real number r is rational if r = a/b for integers a and b with b ≠ 0
Perfect squaren is a perfect square if n = k² for some integer k

Direct proof

A direct proof of \(P \to Q\) assumes \(P\) and uses definitions, algebra and known results to reach \(Q\). It is the natural method when the hypothesis gives you something concrete to work with, such as an equation. The pattern is always the same: write down what the hypothesis means, write down what the conclusion means, and connect them.

Worked problem

A direct proof about odd squares

Prove: if n is an odd integer, then n² is odd.

Show solution
  1. Assume n is odd. By definition, n = 2k + 1 for some integer k.
  2. Then n² = (2k + 1)² = 4k² + 4k + 1.
  3. Rewrite to match the definition of odd: n² = 2(2k² + 2k) + 1.
  4. Since 2k² + 2k is an integer, n² has the form 2m + 1 with m = 2k² + 2k, so n² is odd.

Answer: n² = 2(2k² + 2k) + 1 is odd. The key move was rewriting the result in the exact form the definition requires.

Worked problem

A direct proof about divisibility

Prove: for integers a, b and c, if a | b and b | c, then a | c. (Divisibility is transitive.)

Show solution
  1. Assume a | b and b | c. Then b = am and c = bn for some integers m and n.
  2. Substitute: c = bn = (am)n = a(mn).
  3. Since mn is an integer, c is a times an integer.
  4. By definition, a | c.

Answer: c = a(mn), so a divides c. Note that the two hypotheses need different variable names, m and n; reusing k for both would wrongly force m = n.

The comment at the end of that example points to a frequent error. Each time you use a definition, introduce a fresh variable. Writing \(b = ak\) and \(c = bk\) silently assumes the two multipliers are equal, and the proof would then cover only a special case.

Proof by contrapositive

Sometimes the hypothesis gives you little to work with, while the negation of the conclusion gives you a lot. In that case, prove the contrapositive \(\neg Q \to \neg P\), which lesson 2 showed is logically equivalent to \(P \to Q\). You assume the conclusion is false and show the hypothesis must be false too.

Worked problem

Using the contrapositive

Prove: if n² is even, then n is even.

Show solution
  1. A direct attempt starts from n² = 2k and needs to extract information about n from a square root, which is awkward.
  2. Contrapositive: if n is not even, then n² is not even; that is, if n is odd then n² is odd.
  3. That is exactly the statement proved in the first example of this lesson.
  4. Since the contrapositive is true, the original statement is true.

Answer: Proved via the contrapositive. Choosing the contrapositive turned a hard direct proof into one we had already done.

How do you decide which method to use? Try writing out the hypothesis and the negated conclusion. Whichever gives a concrete form, such as an equation you can substitute into, is usually the better starting point. Statements about something being not divisible, not rational or not prime often have awkward hypotheses, because not having a property is harder to work with than having one; the contrapositive turns them around.

A decision diagram. Start: proving if P then Q. If P gives a usable form such as an equation, use a direct proof: assume P, derive Q. If not Q gives a better form, use the contrapositive: assume not Q, derive not P. If neither, consider contradiction (lesson 6) or cases.
Choosing between direct proof and contrapositive

Worked problem

Contrapositive with sums

Prove: for integers x and y, if x + y is even, then x and y are both even or both odd.

Show solution
  1. Contrapositive: if x and y have different parity (one even, one odd), then x + y is odd.
  2. Without loss of generality let x be even and y odd: x = 2a, y = 2b + 1.
  3. Then x + y = 2a + 2b + 1 = 2(a + b) + 1, which is odd.
  4. The case x odd, y even is symmetric, since addition is commutative.

Answer: Proved. The phrase without loss of generality signals that the other case is identical up to renaming.

If and only if

A biconditional \(P \longleftrightarrow Q\) is proved by proving two conditionals: \(P \to Q\) and \(Q \to P\). Each direction can use a different method. Combining this lesson's results, we have proved that for every integer \(n\), \(n\) is even if and only if \(n^2\) is even: one direction follows from the fact that the square of an even number is even (a one-line direct proof), and the other is the contrapositive example above.

Worked problem

A biconditional proof

Prove: an integer n is odd if and only if n + 1 is even.

Show solution
  1. (→) Assume n is odd: n = 2k + 1. Then n + 1 = 2k + 2 = 2(k + 1), which is even.
  2. (←) Assume n + 1 is even: n + 1 = 2m. Then n = 2m − 1 = 2(m − 1) + 1, which is odd.
  3. Both directions hold, so the biconditional holds.

Answer: n is odd ↔ n + 1 is even, proved in both directions.

Disproving with a counterexample

To show a universal statement \(\forall x\, P(x)\) is false, you need only one \(x\) for which \(P(x)\) is false. This is the quantifier negation rule of lesson 4 put to work. A counterexample must be specific and checkable: give the value and show the calculation. Python is an excellent counterexample hunter, because it can test thousands of cases in a second.

Searching for a counterexample
# Claim: for every positive integer n, n**2 - n + 11 is prime.
def is_prime(m):
    if m < 2:
        return False
    return all(m % d for d in range(2, int(m ** 0.5) + 1))

for n in range(1, 100):
    v = n * n - n + 11
    if not is_prime(v):
        print(f"counterexample: n = {n}, n^2 - n + 11 = {v} = 11 * {v // 11}")
        break
Output
counterexample: n = 11, n^2 - n + 11 = 121 = 11 * 11

Here \(n = 11\) gives \(121 - 11 + 11 = 121 = 11^2\). You can also see why without searching: when \(n = 11\), every term has a factor of 11. Spotting the reason behind a counterexample often leads to a correct, weaker theorem.

A counterexample disproves only universal claims. An existential claim, \(\exists x\, P(x)\), is proved by exhibiting a single witness and disproved only by showing that no \(x\) works, which is a universal statement requiring proof. Do not confuse the two directions: one example can prove an existential claim or refute a universal one, but it can never prove a universal claim.

What it takes to prove or disprove quantified statements
To ...a statement of the form ...you need ...
prove∀x P(x)an argument for an arbitrary x
disprove∀x P(x)one counterexample
prove∃x P(x)one witness (or an indirect argument)
disprove∃x P(x)a proof that ∀x ¬P(x)

Writing proofs well

A proof is a piece of writing. In this course and in later ones, a proof is expected to be in complete sentences that a classmate could follow. A few conventions help readers.

Conventions for clear proofs
ConventionExample
State what you will prove and howWe prove the contrapositive: if n is odd, then n² is odd.
Introduce every variableLet n be an arbitrary odd integer. Then n = 2k + 1 for some integer k.
Justify each step brieflyby the definition of divisibility; by the previous lemma; by algebra
Use fresh variables for each use of a definitionb = am and c = bn, not b = ak and c = bk
End clearlyTherefore n² is odd, as required. Or use the symbol ∎.
Avoid starting from the conclusionDo not assume Q and then derive something true; that proves nothing.

The last convention addresses the most serious logical error in student proofs: starting from what you want to prove and working toward something true. Deriving a true statement from \(Q\) shows only \(Q \to \text{true}\), which is always true and says nothing about \(Q\). For instance, from \(1 = 2\) you can multiply both sides by 0 to get \(0 = 0\), which is true, but that does not make \(1 = 2\) true. Working backward is fine as scratch work to find a proof, but the final proof must run forward from the hypothesis.

Worked problem

Spotting a backwards proof

A student "proves" that for all real a, b, (a + b)/2 ≥ √(ab) when a, b ≥ 0 by writing: (a + b)/2 ≥ √(ab), so (a + b)² ≥ 4ab, so a² − 2ab + b² ≥ 0, so (a − b)² ≥ 0, which is true. What is wrong, and how is it fixed?

Show solution
  1. The argument starts from the conclusion and derives a true statement, which proves nothing on its own.
  2. Fix: start from the true fact (a − b)² ≥ 0 and reverse each step.
  3. Expand: a² − 2ab + b² ≥ 0, so a² + 2ab + b² ≥ 4ab, so (a + b)² ≥ 4ab.
  4. Both sides are non-negative, so taking square roots gives a + b ≥ 2√(ab), and dividing by 2 gives the result.

Answer: The steps happen to be reversible here, but the proof must be written forward and must check each reversal (taking square roots needs non-negativity).

Proof checking with Python

Python cannot prove a universal statement over an infinite domain, but it can check a proof's algebra. A symbolic algebra library such as SymPy can confirm that \((2k+1)^2 = 2(2k^2+2k)+1\). With only the standard library you can at least test the identity on many values, which catches slips in algebra before you rely on them.

Testing the algebra used in a proof (evidence, not proof)
import random
random.seed(272)
ok = all((2*k + 1)**2 == 2*(2*k*k + 2*k) + 1 for k in (random.randint(-10**6, 10**6) for _ in range(10000)))
print("identity (2k+1)^2 = 2(2k^2+2k)+1 holds on 10,000 random k:", ok)
# Divisibility transitivity, checked on random triples a | b, b | c
trials = 0
for _ in range(10000):
    a = random.randint(1, 50); m = random.randint(-50, 50); n = random.randint(-50, 50)
    b, c = a * m, a * m * n
    assert c % a == 0
    trials += 1
print("transitivity of divisibility held in", trials, "random cases")
Output
identity (2k+1)^2 = 2(2k^2+2k)+1 holds on 10,000 random k: True
transitivity of divisibility held in 10000 random cases

Common mistakes

  • MistakeProving a universal statement with one example.

    InsteadUse an arbitrary element and general reasoning; examples only illustrate.

  • MistakeReusing the same variable for two different uses of a definition.

    InsteadIntroduce a fresh variable each time: b = am, c = bn.

  • MistakeStarting from the conclusion and deriving something true.

    InsteadRun the proof forward from the hypothesis; backward work is only scratch work.

  • MistakeProving the converse instead of the contrapositive.

    InsteadThe contrapositive of P → Q is ¬Q → ¬P; the converse Q → P is a different statement.

  • MistakeProving only one direction of an if and only if.

    InsteadProve both P → Q and Q → P.

  • MistakeGiving a counterexample without showing the check.

    InsteadState the value and show the calculation that makes the claim fail.

Summary and key terms

  • To prove ∀x (P(x) → Q(x)), let x be arbitrary, assume P(x) and derive Q(x).
  • A direct proof assumes the hypothesis and works forward using definitions and algebra.
  • A proof by contrapositive assumes ¬Q and derives ¬P; it helps when ¬Q gives a concrete form.
  • A biconditional needs proofs of both directions.
  • One counterexample disproves a universal claim; one witness proves an existential claim.
  • Introduce fresh variables, justify steps, and never argue from the conclusion.
  • Python can find counterexamples and test algebra, but cannot prove claims over infinite domains.
Direct proof
A proof of P → Q that assumes P and derives Q.
Proof by contrapositive
A proof of P → Q that assumes ¬Q and derives ¬P.
Universal generalization
Concluding ∀x P(x) from a proof of P(x) for an arbitrary x.
Divides
a | b means b = ak for some integer k.
Parity
Whether an integer is even or odd.
Without loss of generality
A phrase signalling that the remaining cases are identical up to renaming.
Biconditional proof
A proof of P ↔ Q by proving both P → Q and Q → P.
Witness
A specific value that makes an existential statement true.
Counterexample
A specific value that makes a universal statement false.
QED
Latin quod erat demonstrandum, marking the end of a proof; often written ∎.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Prove: if n is even, then n² is even.

    Show answer

    n = 2k, so n² = 4k² = 2(2k²), which is even.

  2. Prove: the sum of an even and an odd integer is odd.

    Show answer

    2a + (2b + 1) = 2(a + b) + 1.

  3. Prove by contrapositive: if 3n + 2 is odd, then n is odd.

    Show answer

    If n is even, n = 2k, so 3n + 2 = 6k + 2 = 2(3k + 1), which is even.

  4. Prove: if a | b, then a | bc for every integer c.

    Show answer

    b = ak, so bc = a(kc).

  5. Disprove: every odd number greater than 1 is prime.

    Show answer

    9 = 3 × 3 is odd and composite.

  6. Prove: n is even if and only if n + 2 is even.

    Show answer

    (→) n = 2k gives n + 2 = 2(k + 1). (←) n + 2 = 2m gives n = 2(m − 1).

  7. What is wrong with: let a | b and b | c, so b = ak and c = bk?

    Show answer

    It uses the same k twice, which only covers cases where both multipliers are equal.

  8. State the contrapositive of: if xy is irrational then x or y is irrational.

    Show answer

    If x and y are both rational, then xy is rational.

  9. Prove that contrapositive.

    Show answer

    x = a/b and y = c/d give xy = ac/(bd), a ratio of integers with bd ≠ 0.

  10. Is 'there exists a prime p with p + 2 also prime' proved by p = 3?

    Show answer

    Yes; 3 and 5 are both prime, so one witness proves the existential claim.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz