Lecture 8. Number theory
Overview
The integers are the oldest object of mathematics and the natural place to practise the proof methods of Lecture 7. Everything here rests on a single relation — divisibility — yet it grows into primes, unique factorization, the Euclidean algorithm, and modular arithmetic, which in turn underpin hashing, error-correcting codes, and public-key cryptography. Two threads from earlier lectures reappear: congruence modulo is an equivalence relation (Lecture 3), and each theorem below is established by a direct proof, a proof by contradiction, or induction (Lecture 7).
By the end of this chapter you should be able to:
- use divisibility and the division algorithm, and reason about parity;
- test primality by trial division to and prove the infinitude of primes;
- compute with the Euclidean algorithm and recover Bézout coefficients with its extended form;
- state and use the Fundamental Theorem of Arithmetic and the identity ;
- compute with congruences, find modular inverses, and solve linear congruences;
- apply Euler’s and Fermat’s theorems and the totient , and combine congruences with the Chinese Remainder Theorem;
- use the pigeonhole principle to force repetition.
8.1 Divisibility and parity
Definition (divisibility). For integers and , we say divides , written , if for some integer . Then is a divisor of and a multiple of . An integer is even if and odd otherwise.
Proposition 8.1 (basic laws). For all integers :
- ; if and then (transitivity);
- if and then for all integers .
Proof. (1) . If and then . (2) Write , ; then .
Part (2) — every linear combination of and is divisible by any common divisor — is used constantly below.
8.2 The division algorithm
Theorem 8.2 (division algorithm). For integers and there exist unique integers (quotient) and (remainder) with
Proof (existence). Consider . is nonempty and bounded below by , so by well-ordering it has a least element . If then is a smaller member of , a contradiction; hence . Uniqueness: if then , so ; but , forcing and then .
We write . Parity is the case .
8.3 Primes and the infinitude of primes
Definition (prime). An integer is prime if its only positive divisors are and ; otherwise it is composite.

Lemma 8.3. Every integer has a prime divisor.
Proof (strong induction / least criminal). If is prime, done. Otherwise with ; by the induction hypothesis has a prime divisor , and .
Theorem 8.4 (Euclid). There are infinitely many primes.
Proof (by contradiction). Suppose the primes are exactly . Let . Since , by Lemma 8.3 it has a prime divisor . That equals some ; but , so if then — impossible. Hence is a prime outside the list, a contradiction.
Proposition 8.5 (primality by trial division). If has no prime divisor , then is prime.
Proof. If with then , and has a prime divisor . Contrapositive gives the claim.
This proposition is exactly the test implemented in Lab 5.
8.4 The greatest common divisor and Euclid’s algorithm
Definition (gcd). The greatest common divisor of integers (not both ) is the largest integer dividing both. Integers with are coprime.
Lemma 8.6 (the key identity). For , .
Proof. Write with . If and then ; conversely if and then . So and have the same common divisors, hence the same greatest one.
Iterating Lemma 8.6 until the remainder is is the Euclidean algorithm; it terminates because the remainders strictly decrease, and the last nonzero remainder is . For example . The number of steps is .

Theorem 8.7 (Bézout). There exist integers with .
Proof. Run the Euclidean algorithm and back-substitute; each remainder is a linear combination of and , so the last nonzero one — the gcd — is too. (This is the extended Euclidean algorithm.)
Worked example (extended Euclidean algorithm). Take , . Forward:
so . Back-substitute from the last nonzero remainder:
Thus solves ; read modulo it gives the inverse (used in §8.9).
8.5 The least common multiple
Definition (lcm). is the smallest positive common multiple of and .
Theorem 8.8. For positive ,
Proof. Write the prime factorizations and (§8.6). For each prime , the exponent of in is and in is . Since for every , multiplying over all primes gives .
Hence ; e.g. .
8.6 The Fundamental Theorem of Arithmetic
Lemma 8.9 (Euclid’s lemma). If is prime and , then or .
Proof. If then , so by Bézout . Multiply by : . Now and , so .
Theorem 8.10 (Fundamental Theorem of Arithmetic). Every integer factors into primes, and the factorization is unique up to order:
Proof. Existence is Lemma 8.3 applied repeatedly (strong induction). Uniqueness: given two factorizations, a prime on the left divides the right side, so by Euclid’s lemma it equals some prime there; cancel it and induct.
For example (the format produced in Lab 5).

8.7 Modular arithmetic and congruences
Definition (congruence). For , is congruent to modulo , written , if .
Theorem 8.11. Congruence modulo is an equivalence relation on .
Proof. Reflexive: . Symmetric: . Transitive: if and then, by Proposition 8.1(2), .
This is the promised callback to Lecture 3: the equivalence classes are the residue classes , and they partition . Congruences may be added and multiplied: if and , then and — which is why one can reduce mod at any point in a calculation.
8.8 Euler’s totient function
Definition (totient). is the number of integers in that are coprime to .
For a prime power, (the multiples of are the only non-coprime residues). Moreover is multiplicative on coprime arguments — whenever — because the Chinese Remainder Theorem (§8.9) matches the residues modulo bijectively with pairs of residues modulo and modulo , and such a pair is coprime to exactly when both coordinates are coprime to their moduli. Factoring therefore gives
For example , counting — the value computed in Lab 5.

8.9 Modular inverses, and the theorems of Euler and Fermat
Modular inverse. An inverse of modulo is an integer with . It exists iff : Bézout (Theorem 8.7) gives , and reducing mod leaves , so — and the extended Euclidean algorithm computes . Example: , so . This is also why a common factor may be cancelled from a congruence only when it is coprime to the modulus: , yet .
Theorem 8.12 (Euler). If then .
Proof. Let be the residues coprime to . Multiplication by permutes this set modulo (it is a bijection, with inverse “multiply by ”), so , i.e. . Each is invertible, so the product cancels, leaving .
Corollary 8.13 (Fermat’s little theorem). If is prime and , then (since ); equivalently for every integer .
These are the engine of public-key cryptography: in RSA a message is encrypted by raising it to a public exponent modulo , and decrypted by raising the result to — a modular inverse — with Euler’s theorem guaranteeing the round trip recovers the original.
Linear congruences. The congruence is solvable iff ; when the solution is unique modulo , namely . For example gives .
Fast modular exponentiation. To evaluate cheaply, use square-and-multiply: scan the bits of , squaring the running value at each step and multiplying in on every -bit — multiplications instead of . This is what makes RSA practical for large exponents.
Chinese Remainder Theorem (CRT). If are pairwise coprime with product , the system has a unique solution modulo :
(Each is coprime to , so exists; the -th term is modulo and modulo every other , and two solutions differ by a common multiple of all the , hence of .) Example: and give .
8.10 The pigeonhole principle
A small but powerful counting tool, and a favourite source of slick proofs.
Theorem 8.14 (pigeonhole principle). If objects are placed into boxes, some box contains at least two objects.
Proof. If every box held at most one object, the total would be at most .
Corollary 8.15. Among any integers, two are congruent modulo .
Proof. There are only residue classes modulo (§8.7); with integers, two share a class.
This corollary is the engine behind arguments that force repetition — for instance the pumping lemma for finite automata (Lecture 14), where among the states visited along a long run, two must coincide.
Exercises
Warm-up.
-
List all divisors of and give its prime factorization.
-
Compute by the Euclidean algorithm, showing each step, then .
-
Which residue class modulo contains ?
-
Find the inverse .
Standard.
- Prove that if and then .
- Compute using the product formula.
- Prove that is even for every integer (a direct proof; cf. Lecture 7).
- Use the extended Euclidean algorithm (Bézout’s identity) to find integers with .
- Solve the linear congruence , listing all solutions modulo .
- Use Fermat’s little theorem to compute .
Challenge.
- Prove that there are infinitely many primes of the form . (Adapt Euclid’s argument in §8.3.)
- Show that and with imply .
- Solve the system , , with the Chinese Remainder Theorem.
Selected answers & hints
- ; the nine divisors are .
- , , , so ; then .
- , since .
- , so .
- If both are nonzero, and give , so and ; hence . (If either is , divisibility forces the other to be as well.)
- , so .
- is a product of two consecutive integers, one of which is even, so the product is even.
- , , so ; thus .
- divides , so there are two solutions modulo . Dividing through by : , and , so ; hence or .
- (Fermat) and , so .
- Suppose only finitely many such primes (all ), and put . Then , so — since a product of primes that are all would itself be — has a prime factor . But divides no (each divides ), so is a new prime of that form, a contradiction.
- and ; write Bézout . Then , and divides both terms (the first because , the second because ), so .
- From and we get (indeed , ); and already holds, so .
Chapter summary
- Divisibility and the division algorithm are the foundation; common divisors survive linear combinations.
- Primes are the multiplicative atoms: every has a prime divisor, there are infinitely many primes (Euclid), and trial division to decides primality.
- The Euclidean algorithm computes in steps; Bézout and Euclid’s lemma give the Fundamental Theorem of Arithmetic — unique prime factorization — and .
- Congruence mod is an equivalence relation partitioning into residue classes; Euler’s totient counts the coprime residues.
- The pigeonhole principle forces repetition — a proof tool reused for automata (Lecture 14).
Cross-references within this course: Lecture 3 (relations and equivalence — congruence is an equivalence relation), Lecture 7 (methods of proof — every result here is a direct/contradiction/induction proof), and Laboratory Work 5, which implements parity, primality, GCD/LCM, factorization, and the totient.
Further reading
- K. Rosen, Discrete Mathematics and Its Applications — number theory chapters.
- G. H. Hardy, E. M. Wright, An Introduction to the Theory of Numbers.
- V. Shoup, A Computational Introduction to Number Theory and Algebra.