Raw

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 nn 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 n\sqrt{n} and prove the infinitude of primes;
  • compute gcd\gcd with the Euclidean algorithm and recover Bézout coefficients with its extended form;
  • state and use the Fundamental Theorem of Arithmetic and the identity gcdlcm=ab\gcd\cdot\operatorname{lcm}=ab;
  • compute with congruences, find modular inverses, and solve linear congruences;
  • apply Euler’s and Fermat’s theorems and the totient φ(n)\varphi(n), and combine congruences with the Chinese Remainder Theorem;
  • use the pigeonhole principle to force repetition.

8.1 Divisibility and parity

Definition (divisibility). For integers aa and bb, we say bb divides aa, written bab \mid a, if a=bka = b\,k for some integer kk. Then bb is a divisor of aa and aa a multiple of bb. An integer is even if 2n2 \mid n and odd otherwise.

Proposition 8.1 (basic laws). For all integers a,b,ca,b,c:

  1. aaa \mid a; if aba \mid b and bcb \mid c then aca \mid c (transitivity);
  2. if dad \mid a and dbd \mid b then d(ax+by)d \mid (ax + by) for all integers x,yx,y.

Proof. (1) a=a1a = a\cdot 1. If b=akb = a k and c=bc = b\ell then c=a(k)c = a(k\ell). (2) Write a=dma = d m, b=dnb = d n; then ax+by=d(mx+ny)ax + by = d(mx + ny). \blacksquare

Part (2) — every linear combination of aa and bb is divisible by any common divisor — is used constantly below.

8.2 The division algorithm

Theorem 8.2 (division algorithm). For integers aa and b>0b > 0 there exist unique integers qq (quotient) and rr (remainder) with

a=bq+r,0r<b.a = b q + r, \qquad 0 \le r < b.

Proof (existence). Consider S={abq:qZ, abq0}S = \{a - bq : q \in \mathbb{Z},\ a - bq \ge 0\}. SS is nonempty and bounded below by 00, so by well-ordering it has a least element r=abqr = a - bq. If rbr \ge b then ab(q+1)=rb0a - b(q+1) = r - b \ge 0 is a smaller member of SS, a contradiction; hence 0r<b0 \le r < b. Uniqueness: if a=bq1+r1=bq2+r2a = bq_1 + r_1 = bq_2 + r_2 then b(q1q2)=r2r1b(q_1 - q_2) = r_2 - r_1, so b(r2r1)b \mid (r_2 - r_1); but r2r1<b|r_2 - r_1| < b, forcing r1=r2r_1 = r_2 and then q1=q2q_1 = q_2. \blacksquare

We write r=amodbr = a \bmod b. Parity is the case b=2b = 2.

8.3 Primes and the infinitude of primes

Definition (prime). An integer p>1p > 1 is prime if its only positive divisors are 11 and pp; otherwise it is composite.

Sieve of Eratosthenes: primes up to 31 highlighted

Lemma 8.3. Every integer n>1n > 1 has a prime divisor.

Proof (strong induction / least criminal). If nn is prime, done. Otherwise n=abn = ab with 1<a<n1 < a < n; by the induction hypothesis aa has a prime divisor pp, and panp \mid a \mid n. \blacksquare

Theorem 8.4 (Euclid). There are infinitely many primes.

Proof (by contradiction). Suppose the primes are exactly p1,,pkp_1, \dots, p_k. Let N=p1p2pk+1N = p_1 p_2 \cdots p_k + 1. Since N>1N > 1, by Lemma 8.3 it has a prime divisor pp. That pp equals some pip_i; but pi(N1)p_i \mid (N - 1), so if piNp_i \mid N then pi(N(N1))=1p_i \mid \bigl(N - (N-1)\bigr) = 1 — impossible. Hence pp is a prime outside the list, a contradiction. \blacksquare

Proposition 8.5 (primality by trial division). If n>1n > 1 has no prime divisor pnp \le \sqrt{n}, then nn is prime.

Proof. If n=abn = ab with 1<ab1 < a \le b then ana \le \sqrt{n}, and aa has a prime divisor an\le a \le \sqrt{n}. Contrapositive gives the claim. \blacksquare

This proposition is exactly the O(n)O(\sqrt{n}) test implemented in Lab 5.

8.4 The greatest common divisor and Euclid’s algorithm

Definition (gcd). The greatest common divisor gcd(a,b)\gcd(a,b) of integers (not both 00) is the largest integer dividing both. Integers with gcd(a,b)=1\gcd(a,b) = 1 are coprime.

Lemma 8.6 (the key identity). For b0b \ne 0, gcd(a,b)=gcd(b, amodb)\gcd(a,b) = \gcd(b,\ a \bmod b).

Proof. Write a=bq+ra = bq + r with r=amodbr = a \bmod b. If dad \mid a and dbd \mid b then d(abq)=rd \mid (a - bq) = r; conversely if dbd \mid b and drd \mid r then d(bq+r)=ad \mid (bq + r) = a. So {a,b}\{a,b\} and {b,r}\{b,r\} have the same common divisors, hence the same greatest one. \blacksquare

Iterating Lemma 8.6 until the remainder is 00 is the Euclidean algorithm; it terminates because the remainders strictly decrease, and the last nonzero remainder is gcd(a,b)\gcd(a,b). For example gcd(48,18)=gcd(18,12)=gcd(12,6)=gcd(6,0)=6\gcd(48,18) = \gcd(18,12) = \gcd(12,6) = \gcd(6,0) = 6. The number of steps is O(logmin(a,b))O(\log \min(a,b)).

Euclidean algorithm: gcd(48, 18) = 6 in three steps

Theorem 8.7 (Bézout). There exist integers x,yx,y with gcd(a,b)=ax+by\gcd(a,b) = ax + by.

Proof. Run the Euclidean algorithm and back-substitute; each remainder ri=ri2qri1r_i = r_{i-2} - q\, r_{i-1} is a linear combination of aa and bb, so the last nonzero one — the gcd — is too. (This is the extended Euclidean algorithm.) \blacksquare

Worked example (extended Euclidean algorithm). Take a=30a = 30, b=7b = 7. Forward:

30=47+2,7=32+1,2=21+0,30 = 4\cdot 7 + 2, \qquad 7 = 3\cdot 2 + 1, \qquad 2 = 2\cdot 1 + 0,

so gcd(30,7)=1\gcd(30,7) = 1. Back-substitute from the last nonzero remainder:

1=732=73(3047)=137330.1 = 7 - 3\cdot 2 = 7 - 3\,(30 - 4\cdot 7) = 13\cdot 7 - 3\cdot 30 .

Thus (x,y)=(3,13)(x,y) = (-3, 13) solves 30x+7y=130x + 7y = 1; read modulo 3030 it gives the inverse 7113(mod30)7^{-1} \equiv 13 \pmod{30} (used in §8.9).

8.5 The least common multiple

Definition (lcm). lcm(a,b)\operatorname{lcm}(a,b) is the smallest positive common multiple of aa and bb.

Theorem 8.8. For positive a,ba,b,  gcd(a,b)lcm(a,b)=ab.\ \gcd(a,b)\cdot\operatorname{lcm}(a,b) = a b.

Proof. Write the prime factorizations a=ppapa = \prod_p p^{\,a_p} and b=ppbpb = \prod_p p^{\,b_p} (§8.6). For each prime pp, the exponent of pp in gcd(a,b)\gcd(a,b) is min(ap,bp)\min(a_p, b_p) and in lcm(a,b)\operatorname{lcm}(a,b) is max(ap,bp)\max(a_p, b_p). Since min(ap,bp)+max(ap,bp)=ap+bp\min(a_p,b_p) + \max(a_p,b_p) = a_p + b_p for every pp, multiplying over all primes gives gcd(a,b)lcm(a,b)=ppap+bp=ab\gcd(a,b)\cdot\operatorname{lcm}(a,b) = \prod_p p^{\,a_p + b_p} = ab. \blacksquare

Hence lcm(a,b)=ab/gcd(a,b)\operatorname{lcm}(a,b) = ab/\gcd(a,b); e.g. lcm(15,20)=300/5=60\operatorname{lcm}(15,20) = 300/5 = 60.

8.6 The Fundamental Theorem of Arithmetic

Lemma 8.9 (Euclid’s lemma). If pp is prime and pabp \mid ab, then pap \mid a or pbp \mid b.

Proof. If pap \nmid a then gcd(p,a)=1\gcd(p,a) = 1, so by Bézout 1=px+ay1 = px + ay. Multiply by bb: b=pbx+abyb = pbx + aby. Now ppbxp \mid pbx and pababyp \mid ab \mid aby, so pbp \mid b. \blacksquare

Theorem 8.10 (Fundamental Theorem of Arithmetic). Every integer n>1n > 1 factors into primes, and the factorization is unique up to order:

n=p1e1p2e2pkek.n = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}.

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. \blacksquare

For example 56=23756 = 2^3 \cdot 7 (the format produced in Lab 5).

Factor tree: 56 = 2 times 2 times 2 times 7

8.7 Modular arithmetic and congruences

Definition (congruence). For n>0n > 0, aa is congruent to bb modulo nn, written ab(modn)a \equiv b \pmod{n}, if n(ab)n \mid (a - b).

Theorem 8.11. Congruence modulo nn is an equivalence relation on Z\mathbb{Z}.

Proof. Reflexive: n0=aan \mid 0 = a - a. Symmetric: n(ab)n(ba)n \mid (a-b) \Rightarrow n \mid (b-a). Transitive: if n(ab)n \mid (a-b) and n(bc)n \mid (b-c) then, by Proposition 8.1(2), n((ab)+(bc))=acn \mid \bigl((a-b)+(b-c)\bigr) = a - c. \blacksquare

This is the promised callback to Lecture 3: the equivalence classes are the nn residue classes [0],[1],,[n1][0], [1], \dots, [n-1], and they partition Z\mathbb{Z}. Congruences may be added and multiplied: if aaa \equiv a' and bb(modn)b \equiv b' \pmod n, then a+ba+ba + b \equiv a' + b' and abab(modn)ab \equiv a'b' \pmod n — which is why one can reduce mod nn at any point in a calculation.

8.8 Euler’s totient function

Definition (totient). φ(n)\varphi(n) is the number of integers in {1,,n}\{1, \dots, n\} that are coprime to nn.

For a prime power, φ(pk)=pkpk1=pk ⁣(11p)\varphi(p^k) = p^k - p^{k-1} = p^k\!\left(1 - \tfrac1p\right) (the multiples of pp are the only non-coprime residues). Moreover φ\varphi is multiplicative on coprime arguments — φ(mn)=φ(m)φ(n)\varphi(mn) = \varphi(m)\varphi(n) whenever gcd(m,n)=1\gcd(m,n) = 1 — because the Chinese Remainder Theorem (§8.9) matches the residues modulo mnmn bijectively with pairs of residues modulo mm and modulo nn, and such a pair is coprime to mnmn exactly when both coordinates are coprime to their moduli. Factoring n=p1e1pkekn = p_1^{e_1}\cdots p_k^{e_k} therefore gives

φ(n)=ni=1k(11pi).\varphi(n) = n \prod_{i=1}^{k}\left(1 - \frac{1}{p_i}\right).

For example φ(12)=12(112)(113)=4\varphi(12) = 12\left(1-\tfrac12\right)\left(1-\tfrac13\right) = 4, counting {1,5,7,11}\{1,5,7,11\} — the value computed in Lab 5.

Euler's totient phi(12) = 4: coprime residues 1, 5, 7, 11

8.9 Modular inverses, and the theorems of Euler and Fermat

Modular inverse. An inverse of aa modulo nn is an integer a1a^{-1} with aa11(modn)a\,a^{-1}\equiv 1\pmod n. It exists iff gcd(a,n)=1\gcd(a,n)=1: Bézout (Theorem 8.7) gives ax+ny=1ax+ny=1, and reducing mod nn leaves ax1ax\equiv 1, so a1xa^{-1}\equiv x — and the extended Euclidean algorithm computes xx. Example: 35=151(mod7)3\cdot 5=15\equiv 1 \pmod 7, so 315(mod7)3^{-1}\equiv 5\pmod 7. This is also why a common factor may be cancelled from a congruence only when it is coprime to the modulus: 2320(mod6)2\cdot 3\equiv 2\cdot 0\pmod 6, yet 3≢0(mod6)3\not\equiv 0\pmod 6.

Theorem 8.12 (Euler). If gcd(a,n)=1\gcd(a,n)=1 then aφ(n)1(modn)a^{\varphi(n)}\equiv 1\pmod n.

Proof. Let r1,,rφ(n)r_1,\dots,r_{\varphi(n)} be the residues coprime to nn. Multiplication by aa permutes this set modulo nn (it is a bijection, with inverse “multiply by a1a^{-1}”), so i(ari)iri(modn)\prod_i (a r_i)\equiv \prod_i r_i \pmod n, i.e. aφ(n)iriiria^{\varphi(n)}\prod_i r_i\equiv \prod_i r_i. Each rir_i is invertible, so the product iri\prod_i r_i cancels, leaving aφ(n)1a^{\varphi(n)}\equiv 1. \blacksquare

Corollary 8.13 (Fermat’s little theorem). If pp is prime and pap\nmid a, then ap11(modp)a^{p-1}\equiv 1\pmod p (since φ(p)=p1\varphi(p)=p-1); equivalently apa(modp)a^{p}\equiv a\pmod p for every integer aa.

These are the engine of public-key cryptography: in RSA a message is encrypted by raising it to a public exponent ee modulo n=pqn=pq, and decrypted by raising the result to de1(modφ(n))d\equiv e^{-1}\pmod{\varphi(n)} — a modular inverse — with Euler’s theorem guaranteeing the round trip recovers the original.

Linear congruences. The congruence axb(modn)ax\equiv b\pmod n is solvable iff gcd(a,n)b\gcd(a,n)\mid b; when gcd(a,n)=1\gcd(a,n)=1 the solution is unique modulo nn, namely xa1bx\equiv a^{-1}b. For example 3x2(mod7)3x\equiv 2\pmod 7 gives x523x\equiv 5\cdot 2\equiv 3.

Fast modular exponentiation. To evaluate aemodna^{e}\bmod n cheaply, use square-and-multiply: scan the bits of ee, squaring the running value at each step and multiplying in aa on every 11-bit — O(loge)O(\log e) multiplications instead of ee. This is what makes RSA practical for large exponents.

Chinese Remainder Theorem (CRT). If n1,,nkn_1,\dots,n_k are pairwise coprime with product N=n1nkN=n_1\cdots n_k, the system xai(modni)x\equiv a_i\pmod{n_i} (i=1,,k)(i=1,\dots,k) has a unique solution modulo NN:

xi=1kaiMiMi1(modN),Mi=Nni,MiMi11(modni).x\equiv \sum_{i=1}^{k} a_i\,M_i\,M_i^{-1}\pmod N,\qquad M_i=\tfrac{N}{n_i},\quad M_iM_i^{-1}\equiv 1\pmod{n_i}.

(Each MiM_i is coprime to nin_i, so Mi1M_i^{-1} exists; the ii-th term is ai\equiv a_i modulo nin_i and 0\equiv 0 modulo every other njn_j, and two solutions differ by a common multiple of all the nin_i, hence of NN.) Example: x2 (mod 3)x\equiv 2\ (\mathrm{mod}\ 3) and x3 (mod 5)x\equiv 3\ (\mathrm{mod}\ 5) give x8(mod15)x\equiv 8\pmod{15}.

8.10 The pigeonhole principle

A small but powerful counting tool, and a favourite source of slick proofs.

Theorem 8.14 (pigeonhole principle). If n+1n+1 objects are placed into nn boxes, some box contains at least two objects.

Proof. If every box held at most one object, the total would be at most n<n+1n < n +1. \blacksquare

Corollary 8.15. Among any n+1n+1 integers, two are congruent modulo nn.

Proof. There are only nn residue classes modulo nn (§8.7); with n+1n+1 integers, two share a class. \blacksquare

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.

  1. List all divisors of 3636 and give its prime factorization.

  2. Compute gcd(84,30)\gcd(84, 30) by the Euclidean algorithm, showing each step, then lcm(84,30)\operatorname{lcm}(84,30).

  3. Which residue class modulo 77 contains 100100?

  4. Find the inverse 51mod115^{-1} \bmod 11.

Standard.

  1. Prove that if aba \mid b and bab \mid a then a=±ba = \pm b.
  2. Compute φ(360)\varphi(360) using the product formula.
  3. Prove that n2nn^2 - n is even for every integer nn (a direct proof; cf. Lecture 7).
  4. Use the extended Euclidean algorithm (Bézout’s identity) to find integers x,yx,y with 17x+5y=117x + 5y = 1.
  5. Solve the linear congruence 6x8(mod10)6x \equiv 8 \pmod{10}, listing all solutions modulo 1010.
  6. Use Fermat’s little theorem to compute 2100mod72^{100} \bmod 7.

Challenge.

  1. Prove that there are infinitely many primes of the form 4k+34k + 3. (Adapt Euclid’s argument in §8.3.)
  2. Show that ab(modm)a \equiv b \pmod{m} and ab(modn)a \equiv b \pmod{n} with gcd(m,n)=1\gcd(m,n)=1 imply ab(modmn)a \equiv b \pmod{mn}.
  3. Solve the system x2(mod3)x \equiv 2 \pmod 3, x3(mod4)x \equiv 3 \pmod 4, x1(mod5)x \equiv 1 \pmod 5 with the Chinese Remainder Theorem.

Selected answers & hints

  1. 36=223236 = 2^2 3^2; the nine divisors are 1,2,3,4,6,9,12,18,361,2,3,4,6,9,12,18,36.
  2. 84=230+2484 = 2\cdot 30 + 24, 30=124+630 = 1\cdot 24 + 6, 24=46+024 = 4\cdot 6 + 0, so gcd=6\gcd = 6; then lcm(84,30)=8430/6=420\operatorname{lcm}(84,30) = 84\cdot 30 / 6 = 420.
  3. [2][2], since 100=147+2100 = 14\cdot 7 + 2.
  4. 59=45=411+11(mod11)5\cdot 9 = 45 = 4\cdot 11 + 1 \equiv 1 \pmod{11}, so 5195^{-1} \equiv 9.
  5. If both are nonzero, b=akb = ak and a=ba = b\ell give a=aka = a k\ell, so k=1k\ell = 1 and k,{±1}k,\ell \in \{\pm 1\}; hence a=±ba = \pm b. (If either is 00, divisibility forces the other to be 00 as well.)
  6. 360=23325360 = 2^3 3^2 5, so φ(360)=360122345=96\varphi(360) = 360\cdot\tfrac12\cdot\tfrac23\cdot \tfrac45 = 96.
  7. n2n=n(n1)n^2 - n = n(n-1) is a product of two consecutive integers, one of which is even, so the product is even.
  8. 17=35+217 = 3\cdot 5 + 2, 5=22+15 = 2\cdot 2 + 1, so 1=522=52(1735)=752171 = 5 - 2\cdot 2 = 5 - 2\,(17 - 3\cdot 5) = 7\cdot 5 - 2\cdot 17; thus (x,y)=(2,7)(x,y) = (-2, 7).
  9. gcd(6,10)=2\gcd(6,10) = 2 divides 88, so there are two solutions modulo 1010. Dividing through by 22: 3x4(mod5)3x \equiv 4 \pmod 5, and 312(mod5)3^{-1} \equiv 2 \pmod 5, so x243(mod5)x \equiv 2\cdot 4 \equiv 3 \pmod 5; hence x3x \equiv 3 or 8(mod10)8 \pmod{10}.
  10. 261(mod7)2^6 \equiv 1 \pmod 7 (Fermat) and 100=616+4100 = 6\cdot 16 + 4, so 210024=162(mod7)2^{100} \equiv 2^4 = 16 \equiv 2 \pmod 7.
  11. Suppose only finitely many such primes p1,,pkp_1,\dots,p_k (all 3\ge 3), and put N=4p1pk1N = 4\,p_1\cdots p_k - 1. Then N3(mod4)N \equiv 3 \pmod 4, so — since a product of primes that are all 1(mod4)\equiv 1 \pmod 4 would itself be 1\equiv 1NN has a prime factor q3(mod4)q \equiv 3 \pmod 4. But qq divides no pip_i (each pip_i divides N+1N+1), so qq is a new prime of that form, a contradiction.
  12. m(ab)m \mid (a-b) and n(ab)n \mid (a-b); write Bézout 1=ms+nt1 = ms + nt. Then ab=(ab)ms+(ab)nta - b = (a-b)ms + (a-b)nt, and mnmn divides both terms (the first because n(ab)n \mid (a-b), the second because m(ab)m \mid (a-b)), so mn(ab)mn \mid (a-b).
  13. From x3(mod4)x \equiv 3 \pmod 4 and x1(mod5)x \equiv 1 \pmod 5 we get x11(mod20)x \equiv 11 \pmod{20} (indeed 11mod4=311 \bmod 4 = 3, 11mod5=111 \bmod 5 = 1); and 112(mod3)11 \equiv 2 \pmod 3 already holds, so x11(mod60)x \equiv 11 \pmod{60}.

Chapter summary

  • Divisibility bab \mid a and the division algorithm a=bq+ra = bq + r are the foundation; common divisors survive linear combinations.
  • Primes are the multiplicative atoms: every n>1n>1 has a prime divisor, there are infinitely many primes (Euclid), and trial division to n\sqrt{n} decides primality.
  • The Euclidean algorithm computes gcd\gcd in O(log)O(\log) steps; Bézout and Euclid’s lemma give the Fundamental Theorem of Arithmetic — unique prime factorization — and gcdlcm=ab\gcd\cdot\operatorname{lcm} = ab.
  • Congruence mod nn is an equivalence relation partitioning Z\mathbb{Z} into nn residue classes; Euler’s totient φ(n)=n(11/p)\varphi(n) = n\prod(1-1/p) 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.

Lectures/CDM-L08.md · 17.3 KB · updated 2026-08-01 20:05