Proofs are at the core of any branch of mathematics.
Proofs of correctness and complexity motivate the implementation and modification of algorithms.
For this reason, proving is a necessary skill in computer science.
You will learn about the types of proof and strategies typical of discrete mathematics: direct, contraposition, contradiction, cases, existence and uniqueness.
Python / SymPy — imports (run once)from sympy import symbols, Rational, sqrt, simplify, Eq, S, isprime, primerange
from sympy.logic.inference import satisfiable
from sympy.abc import x, y, z
from fractions import Fraction
import math
2 / 13
Statements
The vocabulary of proofs.
Proposition
A mathematical statement.
Axiom (postulate)
A proposition regarded as self-evident without proof.
Conjecture (hypothesis)
An unproven proposition that is consistent with current data.
Proof
A rigorous mathematical explanation demonstrating that a proposition is true.
Theorem
A proposition that was proven true by accepted mathematical operations and arguments.
Lemma
A short theorem used in proving a larger theorem.
Corollary
A statement that follows almost immediately from a theorem.
A proof is built from axioms, previously proven theorems, and rules of inference (covered in the Logic lesson on inference).
3 / 13
Direct Proof
In the proposition $p \Rightarrow q$, assume $p$ is true, then derive $q$ step by step (this is modus ponens).
Example — the product of two rationals is rational
Let $x, y \in \mathbb{Q}$. Write $x = \dfrac{a}{b}$, $y = \dfrac{c}{d}$ with $b, d \neq 0$. Then:
$x \cdot y \;=\; \dfrac{a}{b} \cdot \dfrac{c}{d} \;=\; \dfrac{ac}{bd}$ where $bd \neq 0$, therefore rational. $\square$
Python — sanity checkfrom fractions import Fraction
x = Fraction(3, 7)
y = Fraction(5, 2)
print(x * y) # 15/14
print(isinstance(x * y, Fraction)) # True
SymPyfrom sympy import Rational, simplify
x = Rational(3, 7)
y = Rational(5, 2)
z = simplify(x * y)
print(z, z.is_rational) # 15/14 True
Another classic — sum of two even numbers is even
If $a = 2k$ and $b = 2m$ with $k, m \in \mathbb{Z}$, then $a + b = 2(k + m)$ is even. $\square$
4 / 13
Proof by Contraposition
$p \Rightarrow q$ is logically equivalent to its contrapositive $\neg q \Rightarrow \neg p$.
So we assume $q$ is false and derive that $p$ is false.
Example — if $ab < 0$ then $a < 0$ or $b < 0$
Direct statement: $\;ab < 0 \;\Rightarrow\; (a < 0) \vee (b < 0)$.
As a rule of arithmetic, the product of two non-negative numbers is non-negative. $\square$
Python — empirical check on the contrapositiveimport itertools
for a, b in itertools.product(range(0, 10), repeat=2): # a,b ≥ 0
assert a * b >= 0
print("Contrapositive holds on the tested grid ✓")
5 / 13
Proof by Contradiction
To prove a proposition $p$, assume $\neg p$ and reach a contradiction (something obviously false). That forces $p$ to be true.
Example — Euclid: the set of primes is infinite
Assume the set of primes is finite: $L = \{p_1, p_2, \dots, p_n\}$.
Thus $n^2 - 1$ is divisible by 3 in both cases. $\square$
Python — verify on a wide rangefor n in range(-1000, 1001):
if n % 3 != 0:
assert (n*n - 1) % 3 == 0
print("Verified on n in [-1000, 1000] ✓")
The first proof of the four-colour theorem was a proof by exhaustion with 1834 cases — computer-assisted.
7 / 13
Existence Proofs
To show $\exists x \,P(x)$, it suffices to exhibit an example.
Example — satisfiability of a Boolean formula
Show that $(\neg x \vee y \vee z) \wedge (x \vee \neg y \vee z) \wedge (x \vee y \vee \neg z) \wedge (x \vee y \vee z)$ is satisfiable.
SymPy — find a witnessfrom sympy.abc import x, y, z
from sympy.logic.inference import satisfiable
from sympy import And, Or, Not
f = And(Or(Not(x), y, z),
Or(x, Not(y), z),
Or(x, y, Not(z)),
Or(x, y, z))
print(satisfiable(f))
# {x: True, y: True, z: True} ← a witness assignment
Non-constructive existence proofs are also valid — for example, the pigeonhole principle proves that two people in any city of 1 million have the same number of hairs, without identifying them.
8 / 13
Uniqueness Proofs
Show there is only one object fulfilling the given properties.
Standard recipe:
Show existence (at least one object satisfies $P$).
Suppose two such objects exist, $u_1$ and $u_2$, and prove $u_1 = u_2$.
Example — there is only one integer that is both prime and even
Let $p$ be prime. Its only positive divisors are 1 and $p$.
$p$ is also even, so $p$ is divisible by 2. Hence $p \in \{1, p\}$ must contain 2, i.e. $p = 2$.
$\therefore$ $p = 2$ is the unique even prime. $\square$
Python — sanity scanfrom sympy import isprime
even_primes = [n for n in range(2, 1000) if n % 2 == 0 and isprime(n)]
print(even_primes) # [2]