taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Volume P2 · Concrete mathematics

Divisibility, primes, gcd and congruences

Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.

Contents
  1. Divisibility and the greatest common divisor
  2. Primes and factorization
  3. Congruences
  4. The theorems of Fermat, Euler and Wilson

Divisibility and the greatest common divisor

Statement

All letters stand for integers. The section starts with the relation "divides" and with division with remainder. It then defines the greatest common divisor, gives Euclid's algorithm for it, and extends the algorithm so that it also writes the divisor as a sum of multiples of the inputs.

Definition P2.5.1 (divisor). An integer aa divides an integer bb when b=akb = ak for some integer kk. The notation is a∣ba \mid b. Then aa is a divisor of bb, and bb is a multiple of aa. When aa does not divide bb, the notation is a∤ba \nmid b.

Example P2.5.2. 7∣917 \mid 91, since 91=7⋅1391 = 7 \cdot 13. Also −7∣91-7 \mid 91, since 91=(−7)(−13)91 = (-7)(-13). But 7∤1007 \nmid 100: the multiples of 77 near 100100 are 9898 and 105105, and 100100 is neither of them. The divisors of 1212 are ±1\pm 1, ±2\pm 2, ±3\pm 3, ±4\pm 4, ±6\pm 6 and ±12\pm 12.

Example P2.5.3. Each integer aa divides 00, since 0=a⋅00 = a \cdot 0. In the other direction, 0∣b0 \mid b only when b=0b = 0, since 0⋅k=00 \cdot k = 0 for each kk. So 00 is a multiple of every integer, and the only multiple of 00 is 00 itself.

Lemma P2.5.4 (rules of divisibility). For all integers aa, bb, cc, xx and yy:

  • (i) a∣aa \mid a;
  • (ii) if a∣ba \mid b and b∣cb \mid c, then a∣ca \mid c;
  • (iii) if a∣ba \mid b and a∣ca \mid c, then a∣bx+cya \mid bx + cy;
  • (iv) if a∣ba \mid b and b≠0b \ne 0, then ∣a∣≤∣b∣|a| \le |b|;
  • (v) if a∣ba \mid b and b∣ab \mid a, then ∣a∣=∣b∣|a| = |b|.

Lean: dvd_refl, dvd_trans, Dvd.dvd.linear_comb, Nat.le_of_dvd and Nat.dvd_antisymm in Mathlib and the Lean core.

Proof. (i) a=a⋅1a = a \cdot 1. (ii) If b=akb = ak and c=blc = bl, then c=a(kl)c = a(kl). (iii) If b=akb = ak and c=alc = al, then bx+cy=a(kx+ly)bx + cy = a(kx + ly). (iv) If b=akb = ak and b≠0b \ne 0, then k≠0k \ne 0, so ∣k∣≥1|k| \ge 1 and ∣b∣=∣a∣∣k∣≥∣a∣|b| = |a||k| \ge |a|. (v) If a=0a = 0, then b=0b = 0, since a∣ba \mid b means b=0⋅kb = 0 \cdot k. If a≠0a \ne 0, then b≠0b \ne 0 in the same way, and (iv) gives ∣a∣≤∣b∣|a| \le |b| and ∣b∣≤∣a∣|b| \le |a|. □\square

Theorem P2.5.5 (division with remainder). Let aa and mm be integers with m>0m > 0. There is exactly one pair of integers qq and rr with a=qm+r,0≤r<m.a = qm + r, \qquad 0 \le r < m. The pair is q=⌊a/m⌋q = \lfloor a/m \rfloor and r=a mod mr = a \bmod m.

Lean: Int.mul_ediv_add_emod, Int.emod_nonneg and Int.emod_lt_of_pos in the Lean core.

Proof. Existence. Put q=⌊a/m⌋q = \lfloor a/m \rfloor and r=a−qm=a mod mr = a - qm = a \bmod m. The floor satisfies q≤a/m<q+1q \le a/m < q + 1. Multiply by m>0m > 0: qm≤a<qm+mqm \le a < qm + m. Subtract qmqm: 0≤r<m0 \le r < m. Uniqueness. Let a=qm+r=q′m+r′a = qm + r = q'm + r' with both remainders in [0,m)[0, m). Then m(q−q′)=r′−rm(q - q') = r' - r, and ∣r′−r∣<m|r' - r| < m. A multiple of mm with absolute value less than mm is 00. So r=r′r = r', and then q=q′q = q'. □\square

The numbers qq and rr are the quotient and the remainder. Section 4.3 studied a mod ma \bmod m for real aa and mm. Here only integers occur.

Definition P2.5.6 (greatest common divisor). Let mm and nn be integers that are not both 00. Their greatest common divisor gcd⁡(m,n)\gcd(m, n) is the largest integer that divides both mm and nn. Put gcd⁡(0,0)=0\gcd(0, 0) = 0.

Example P2.5.7. The positive divisors of 8484 are 1,2,3,4,6,7,12,14,21,28,42,841, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84. Those of 120120 are 1,2,3,4,5,6,8,10,12,15,20,24,30,40,60,1201, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120. The common ones are 1,2,3,4,6,121, 2, 3, 4, 6, 12. So gcd⁡(84,120)=12\gcd(84, 120) = 12.

Example P2.5.8. For n≠0n \ne 0, gcd⁡(n,0)=∣n∣\gcd(n, 0) = |n|, because each integer divides 00 and the largest divisor of nn is ∣n∣|n|. Also gcd⁡(m,n)=gcd⁡(n,m)=gcd⁡(−m,n)\gcd(m, n) = \gcd(n, m) = \gcd(-m, n), because mm and −m-m have the same divisors. The value 11 always divides both numbers, so gcd⁡(m,n)≥1\gcd(m, n) \ge 1 when mm and nn are not both 00.

Lemma P2.5.9 (the step of Euclid). Let n>0n > 0. An integer dd divides both mm and nn if and only if dd divides both nn and m mod nm \bmod n. Hence gcd⁡(m,n)=gcd⁡(n,m mod n).\gcd(m, n) = \gcd(n, m \bmod n).

Lean: Nat.gcd_rec in the Lean core, in the form gcd⁡(m,n)=gcd⁡(n mod m,m)\gcd(m, n) = \gcd(n \bmod m, m).

Proof. Write m=qn+rm = qn + r with r=m mod nr = m \bmod n. If d∣md \mid m and d∣nd \mid n, then d∣m−qn=rd \mid m - qn = r by rule (iii). If d∣nd \mid n and d∣rd \mid r, then d∣qn+r=md \mid qn + r = m by the same rule. So the two pairs have the same common divisors and the same largest common divisor. □\square

Algorithm P2.5.10 (Euclid). The input is two integers m≥0m \ge 0 and n≥0n \ge 0. The output is gcd⁡(m,n)\gcd(m, n).

gcd(m, n):
    while n != 0:
        (m, n) := (n, m mod n)
    return m

Lean: Nat.gcd in the Lean core.

Proof. Each pass of the loop keeps gcd⁡(m,n)\gcd(m, n) by the step of Euclid, and it makes nn smaller, since m mod n<nm \bmod n < n. A strictly decreasing sequence of nonnegative integers is finite, so the loop stops. At the end n=0n = 0, and gcd⁡(m,0)=m\gcd(m, 0) = m. The Proof part gives the details and a bound on the number of passes. □\square

Theorem P2.5.11 (Bézout's identity). For all integers mm and nn, there are integers aa and bb with am+bn=gcd⁡(m,n).am + bn = \gcd(m, n). Each common divisor of mm and nn divides gcd⁡(m,n)\gcd(m, n). When mm and nn are not both 00, the value gcd⁡(m,n)\gcd(m, n) is the least positive integer of the form am+bnam + bn.

Lean: Nat.gcd_eq_gcd_ab and Nat.dvd_gcd in Mathlib and the Lean core.

Proof. The extended algorithm below finds aa and bb. If d∣md \mid m and d∣nd \mid n, then d∣am+bn=gcd⁡(m,n)d \mid am + bn = \gcd(m, n) by rule (iii). The Proof part also gives a proof that does not use the algorithm. □\square

Algorithm P2.5.12 (extended Euclid). The input is two integers m≥0m \ge 0 and n≥0n \ge 0. The output is g=gcd⁡(m,n)g = \gcd(m, n) and integers aa and bb with am+bn=gam + bn = g. The algorithm keeps two rows (r,a,b)(r, a, b), each with the property r=am+bnr = am + bn.

xgcd(m, n):
    (r0, a0, b0) := (m, 1, 0)
    (r1, a1, b1) := (n, 0, 1)
    while r1 != 0:
        q := floor(r0 / r1)
        (r0, a0, b0, r1, a1, b1) := (r1, a1, b1, r0 - q*r1, a0 - q*a1, b0 - q*b1)
    return (r0, a0, b0)

Lean: Nat.xgcd in Mathlib.

Proof. The column rr runs exactly as (m,n)(m, n) runs in Euclid's algorithm, so it stops with r0=gcd⁡(m,n)r_0 = \gcd(m, n). The new row is the old row 00 minus qq times the old row 11, so the property r=am+bnr = am + bn passes from the old rows to the new one. □\square

Definition P2.5.13 (relatively prime). Two integers mm and nn are relatively prime, or coprime, when gcd⁡(m,n)=1\gcd(m, n) = 1. The notation of GKP94 and of TAOCP is m⊥nm \perp n.

Example P2.5.14. 8⊥158 \perp 15, since the positive divisors of 88 are 1,2,4,81, 2, 4, 8, and only 11 of them divides 1515. But 15⊥̸2115 \not\perp 21, since 33 divides both.

Example P2.5.15. For each nn, n⊥n+1n \perp n + 1. A common divisor dd divides (n+1)−n=1(n + 1) - n = 1, so d=±1d = \pm 1. Also 1⊥n1 \perp n for each nn, and 0⊥n0 \perp n only for n=±1n = \pm 1.

Corollary P2.5.16 (cancelling a coprime factor). If a∣bca \mid bc and a⊥ba \perp b, then a∣ca \mid c.

Lean: Nat.Coprime.dvd_of_dvd_mul_left in the Lean core.

Proof. Bézout gives xa+yb=1xa + yb = 1. Multiply by cc: c=xac+ybcc = xac + ybc. The number aa divides xacxac, and it divides ybcybc because a∣bca \mid bc. So a∣ca \mid c. □\square

Definition P2.5.17 (least common multiple). Let m>0m > 0 and n>0n > 0. Their least common multiple lcm⁡(m,n)\operatorname{lcm}(m, n) is the least positive integer that is a multiple of both mm and nn.

Example P2.5.18. The positive multiples of 66 are 6,12,18,24,…6, 12, 18, 24, \dots, and those of 1010 are 10,20,30,…10, 20, 30, \dots. The first common one is 3030, so lcm⁡(6,10)=30\operatorname{lcm}(6, 10) = 30. Note that gcd⁡(6,10)⋅lcm⁡(6,10)=2⋅30=60=6⋅10\gcd(6, 10) \cdot \operatorname{lcm}(6, 10) = 2 \cdot 30 = 60 = 6 \cdot 10.

Example P2.5.19. If m∣nm \mid n, then lcm⁡(m,n)=n\operatorname{lcm}(m, n) = n. If m⊥nm \perp n, then lcm⁡(m,n)=mn\operatorname{lcm}(m, n) = mn. For instance lcm⁡(8,15)=120\operatorname{lcm}(8, 15) = 120 and lcm⁡(4,12)=12\operatorname{lcm}(4, 12) = 12.

Theorem P2.5.20 (gcd times lcm). For m>0m > 0 and n>0n > 0, gcd⁡(m,n)⋅lcm⁡(m,n)=mn,\gcd(m, n) \cdot \operatorname{lcm}(m, n) = mn, and each common multiple of mm and nn is a multiple of lcm⁡(m,n)\operatorname{lcm}(m, n).

Lean: Nat.gcd_mul_lcm and Nat.lcm_dvd in the Lean core.

Proof. See the Proof part. □\square

Proof

Euclid's algorithm is correct and stops. Let (m0,n0)(m_0, n_0) be the input, and let (mj,nj)(m_j, n_j) be the pair after jj passes of the loop. The invariant is gcd⁡(mj,nj)=gcd⁡(m0,n0).\gcd(m_j, n_j) = \gcd(m_0, n_0). It holds for j=0j = 0. If it holds for jj and nj≠0n_j \ne 0, then the next pair is (nj,mj mod nj)(n_j, m_j \bmod n_j), and the step of Euclid says that this pair has the same gcd. So the invariant holds for j+1j + 1. Next, nj+1=mj mod nj<njn_{j+1} = m_j \bmod n_j < n_j. The values n0>n1>n2>⋯n_0 > n_1 > n_2 > \cdots are nonnegative integers, so there are at most n0+1n_0 + 1 of them, and the loop stops. It stops at a pass tt with nt=0n_t = 0. Then the output is mt=gcd⁡(mt,0)=gcd⁡(m0,n0)m_t = \gcd(m_t, 0) = \gcd(m_0, n_0).

A bound on the number of passes. The count n0+1n_0 + 1 is very weak. Let m0≥n0>0m_0 \ge n_0 > 0. A better bound comes from this fact: if 0<n≤m0 < n \le m, then m mod n<m/2m \bmod n < m/2. Indeed, if n≤m/2n \le m/2, then m mod n<n≤m/2m \bmod n < n \le m/2. If n>m/2n > m/2, then ⌊m/n⌋=1\lfloor m/n \rfloor = 1 and m mod n=m−n<m/2m \bmod n = m - n < m/2. Now look at two passes. They take (m,n)(m, n) to (m mod n, n mod (m mod n))(m \bmod n, \, n \bmod (m \bmod n)), so the first entry of the pair falls from mm to less than m/2m/2 in two passes, once n≤mn \le m. (After the first pass the pair always has n<mn < m.) So after 2k+12k + 1 passes the first entry is at most m0/2km_0 / 2^k or the loop has stopped. The first entry is a positive integer after each pass. So 1≤m0/2k1 \le m_0/2^k for each such kk, and the number of passes is at most 2lg⁡m0+22 \lg m_0 + 2. Chapter 10 gives the exact worst case: consecutive Fibonacci numbers.

Bézout's identity without the algorithm. Let mm and nn be not both 00. Let SS be the set of positive integers of the form am+bnam + bn. The set is not empty: it holds m2+n2m^2 + n^2. By well ordering SS has a least element g=am+bng = am + bn. Divide mm by gg: m=qg+rm = qg + r with 0≤r<g0 \le r < g. Then r=m−qg=(1−qa)m+(−qb)n.r = m - qg = (1 - qa)m + (-qb)n. If r>0r > 0, then r∈Sr \in S and r<gr < g, which is false. So r=0r = 0 and g∣mg \mid m. In the same way g∣ng \mid n. So gg is a common divisor, and g≤gcd⁡(m,n)g \le \gcd(m, n). On the other side, gcd⁡(m,n)\gcd(m, n) divides am+bn=gam + bn = g, so gcd⁡(m,n)≤g\gcd(m, n) \le g by rule (iv). Hence g=gcd⁡(m,n)g = \gcd(m, n). This proves all three claims of the theorem. The case m=n=0m = n = 0 is trivial: gcd⁡(0,0)=0=1⋅0+1⋅0\gcd(0, 0) = 0 = 1 \cdot 0 + 1 \cdot 0.

The extended algorithm is correct. Write the two rows after jj passes as (r0,a0,b0)(r_0, a_0, b_0) and (r1,a1,b1)(r_1, a_1, b_1). The invariant is r0=a0m+b0n,r1=a1m+b1n.r_0 = a_0 m + b_0 n, \qquad r_1 = a_1 m + b_1 n. It holds at the start, since m=1⋅m+0⋅nm = 1 \cdot m + 0 \cdot n and n=0⋅m+1⋅nn = 0 \cdot m + 1 \cdot n. A pass replaces row 00 by row 11, and row 11 by row 00 minus qq times row 11. A difference of two rows with the property has the property, since the property is linear. So the invariant holds after each pass. The column rr changes as (m,n)(m, n) changes in Euclid's algorithm, since r0−qr1=r0 mod r1r_0 - q r_1 = r_0 \bmod r_1 when q=⌊r0/r1⌋q = \lfloor r_0 / r_1 \rfloor. So the loop stops, and at the end r0=gcd⁡(m,n)=a0m+b0nr_0 = \gcd(m, n) = a_0 m + b_0 n.

The gcd times the lcm. Let g=gcd⁡(m,n)g = \gcd(m, n) and write m=gm′m = g m' and n=gn′n = g n'. Then m′⊥n′m' \perp n': a common divisor d>1d > 1 of m′m' and n′n' would make gdgd a common divisor of mm and nn larger than gg. Put L=gm′n′=mn′=m′nL = g m' n' = m n' = m' n. It is a common multiple of mm and nn. Let cc be any common multiple, c=mu=nvc = m u = n v. Then gm′u=gn′vg m' u = g n' v, so m′u=n′vm' u = n' v. Hence n′∣m′un' \mid m' u, and since n′⊥m′n' \perp m', the corollary gives n′∣un' \mid u. Write u=n′wu = n' w. Then c=mn′w=Lwc = m n' w = L w. So each common multiple is a multiple of LL. In particular each positive common multiple is at least LL, and L=lcm⁡(m,n)L = \operatorname{lcm}(m, n). Finally gL=g⋅gm′n′=mng L = g \cdot g m' n' = mn.

Examples

Example P2.5.21. Euclid's algorithm on m=1517m = 1517 and n=943n = 943. Each row shows the pair before a pass, the quotient and the new remainder.

pass mm nn ⌊m/n⌋\lfloor m/n \rfloor m mod nm \bmod n
1 1517 943 1 574
2 943 574 1 369
3 574 369 1 205
4 369 205 1 164
5 205 164 1 41
6 164 41 4 0

After six passes the pair is (41,0)(41, 0), so gcd⁡(1517,943)=41\gcd(1517, 943) = 41. Indeed 1517=37⋅411517 = 37 \cdot 41 and 943=23⋅41943 = 23 \cdot 41, and 37⊥2337 \perp 23. The run has many quotients equal to 11. That is the slow case of the algorithm: the remainders shrink only by a little at each pass.

Example P2.5.22. The extended algorithm on the same input. Each row is a triple (r,a,b)(r, a, b) with r=1517a+943br = 1517a + 943b. Each new row is the row two places up minus qq times the row one place up.

row qq rr aa bb
start 0 1517 1 0
start 1 943 0 1
1 1 574 1 -1
2 1 369 -1 2
3 1 205 2 -3
4 1 164 -3 5
5 1 41 5 -8
6 4 0 -23 37

The output is the row with r=41r = 41: 5⋅1517−8⋅943=7585−7544=415 \cdot 1517 - 8 \cdot 943 = 7585 - 7544 = 41. The last row is a check: −23⋅1517+37⋅943=−34891+34891=0-23 \cdot 1517 + 37 \cdot 943 = -34891 + 34891 = 0. Its entries are ∓n/g\mp n/g and ±m/g\pm m/g, here −943/41=−23-943/41 = -23 and 1517/41=371517/41 = 37.

Example P2.5.23. The numbers aa and bb of Bézout are not unique. If am+bn=gam + bn = g, then also (a+kn/g)m+(b−km/g)n=g(a + k n/g) m + (b - k m/g) n = g for each integer kk. For m=1517m = 1517 and n=943n = 943 the pairs are (5+23k,−8−37k)(5 + 23k, -8 - 37k), for example (28,−45)(28, -45) and (−18,29)(-18, 29). The extended algorithm gives one pair with small entries.

Example P2.5.24. A use of the corollary. Find all integer solutions of 1517x=943y1517x = 943y. Divide by 4141: 37x=23y37x = 23y. Then 37∣23y37 \mid 23y and 37⊥2337 \perp 23, so 37∣y37 \mid y. Write y=37ty = 37t. Then x=23tx = 23t. The solutions are (x,y)=(23t,37t)(x, y) = (23t, 37t) for integers tt, and the least positive common multiple 1517⋅23=943⋅37=348911517 \cdot 23 = 943 \cdot 37 = 34891 is lcm⁡(1517,943)\operatorname{lcm}(1517, 943).

Example P2.5.25. The step of Euclid in Section 1.2.8. Equation 1.2.8(9) says that dd divides FmF_m and FnF_n if and only if it divides Fm mod nF_{m \bmod n} and FnF_n. Equation 1.2.8(8), the same claim with Fm+nF_{m+n} in place of FmF_m, is the step that leads to it. With the invariant of Euclid's algorithm this gives eq. 1.2.8(7): gcd⁡(Fm,Fn)=Fgcd⁡(m,n)\gcd(F_m, F_n) = F_{\gcd(m, n)}. The argument is the proof of correctness above, with FmF_m in place of mm. Chapter 10 gives it in full.

Exercises

  1. [10] Run Euclid's algorithm on m=2310m = 2310 and n=1001n = 1001. Give each pair and the result.
  2. [M18] Find gcd⁡(n2+1,n+1)\gcd(n^2 + 1, n + 1) for each integer n≥0n \ge 0.
  3. [15] Run the extended algorithm on m=391m = 391 and n=299n = 299. Give integers aa and bb with 391a+299b=gcd⁡(391,299)391a + 299b = \gcd(391, 299).
  4. [M20] Prove that gcd⁡(km,kn)=kgcd⁡(m,n)\gcd(km, kn) = k \gcd(m, n) for each integer k≥0k \ge 0.
  5. [M22] Prove that lcm⁡(a,b,c)=abc/gcd⁡(ab,bc,ca)\operatorname{lcm}(a, b, c) = abc / \gcd(ab, bc, ca) for positive aa, bb and cc. Show by an example that gcd⁡(a,b,c)⋅lcm⁡(a,b,c)\gcd(a, b, c) \cdot \operatorname{lcm}(a, b, c) need not equal abcabc.
  6. [12] A machine takes coins of 66 and 1515 units only. Which amounts from 00 to 6060 can it take, with any number of coins of each kind? Which amounts can it take in general?
  7. [M24] Let m>n>0m > n > 0. Prove that the extended algorithm returns aa and bb with ∣a∣≤n/g|a| \le n/g and ∣b∣≤m/g|b| \le m/g, where g=gcd⁡(m,n)g = \gcd(m, n).

Lean

The Lean core defines Nat.gcd by Euclid's algorithm. The step of Euclid is Nat.gcd_rec, and the universal property of the gcd is Nat.dvd_gcd. Mathlib has the extended algorithm as Nat.xgcd, with the identity of Bézout as Nat.gcd_eq_gcd_ab, which states gcd⁡(x,y)=x⋅a+y⋅b\gcd(x, y) = x \cdot a + y \cdot b for the integers aa and bb of Nat.gcdA and Nat.gcdB. The lcm is Nat.lcm, with Nat.gcd_mul_lcm and Nat.lcm_dvd. Coprimality is Nat.Coprime, and the corollary is Nat.Coprime.dvd_of_dvd_mul_left.

These names are about natural numbers. The integer versions are Int.gcd, Int.gcd_eq_gcd_ab and Int.lcm. Division with remainder on the integers uses Int.ediv and Int.emod. Their values agree with the floor and the  mod \bmod of Chapter 4 when the divisor is positive. TAOCP's x mod yx \bmod y for real arguments and for y<0y < 0 is TAOCP.kmod in TAOCP/Notation (tamnd/taocp-lean main). The theorem TAOCP.floor_mul_add_kmod is the equation y⌊x/y⌋+x mod y=xy \lfloor x/y \rfloor + x \bmod y = x, and TAOCP.kmod_intCast_natCast says that TAOCP.kmod agrees with Int.emod for an integer xx and a natural divisor yy.

The Lean code of TAOCP also writes Algorithm 1.1E in its step format, with the command knuth_algorithm, as TAOCP.S1_1.E.method, and proves it. The theorem TAOCP.S1_1.E.terminates says that the method is an algorithm, and TAOCP.S1_1.E.correct says that its output from mm and nn is Nat.gcd m n. The proof of correctness in the Proof part above is the argument that the Lean proof follows.

Notes and history

Euclid's algorithm is in Book VII of Euclid's Elements, about 300 BC, in a form with subtraction in place of division. It is the first algorithm of TAOCP: Algorithm 1.1E, with the flow chart and the proof by induction of Section 1.1. Algorithm 1.2.1E is the extended algorithm, and Section 1.2.1 uses it as the main example of a proof by assertions on a flow chart. The bound of the Proof part is crude. Section 4.5.3 of TAOCP finds the exact worst case, a theorem of Lamé from 1844, and the average number of passes. Bézout's identity is named after Étienne Bézout, who proved the corresponding fact for polynomials in 1779. The identity for integers is older; Bachet gave it in 1624.

The proof of Bézout by the least element of a set is the usual one in modern texts. The proof by the algorithm is the one TAOCP prefers, since it gives aa and bb and a cost. The extended algorithm comes back in Section 4.5.2 of TAOCP, and in the computation of inverses in Section 5.3 of this chapter.

Primes and factorization

Statement

This section proves that each integer n≥2n \ge 2 is a product of primes in exactly one way, that there are infinitely many primes, and that the exponent of a prime pp in n!n! is a sum of floors. The last result is eq. 1.2.5(8). The letter pp always stands for a prime.

Definition P2.5.26 (prime). An integer p≥2p \ge 2 is prime when its only positive divisors are 11 and pp. An integer n≥2n \ge 2 that is not prime is composite. The integer 11 is neither prime nor composite.

Example P2.5.27. The primes below 5050 are 2,3,5,7,11,13,17,19,23,29,31,37,41,43,472, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47. There are 1515 of them. The number 22 is the only even prime, since each other even number has the divisor 22.

Example P2.5.28. The number 9191 looks prime, but 91=7⋅1391 = 7 \cdot 13. To test nn, it is enough to try the divisors dd with 2≤d≤n2 \le d \le \sqrt{n}: if n=abn = ab with 1<a≤b1 < a \le b, then a2≤ab=na^2 \le ab = n. For n=97n = 97 the trial divisors are 2,…,92, \dots, 9, none of them divides 9797, so 9797 is prime.

Lemma P2.5.29 (a least divisor is prime). Each integer n≥2n \ge 2 has a prime divisor. The least divisor d≥2d \ge 2 of nn is prime.

Lean: Nat.exists_prime_and_dvd and Nat.minFac_prime in Mathlib.

Proof. The set of divisors d≥2d \ge 2 of nn holds nn, so it has a least element dd. If dd had a divisor ee with 1<e<d1 < e < d, then e∣ne \mid n by transitivity, and ee would be a smaller divisor of nn. So dd is prime. □\square

Lemma P2.5.30 (Euclid's lemma). If p∣abp \mid ab, then p∣ap \mid a or p∣bp \mid b. More generally, if pp divides a product a1a2⋯aka_1 a_2 \cdots a_k, then it divides one of the factors.

Lean: Nat.Prime.dvd_mul in Mathlib.

Proof. Let p∣abp \mid ab and p∤ap \nmid a. The gcd of pp and aa is a positive divisor of pp, so it is 11 or pp. It is not pp, since p∤ap \nmid a. So p⊥ap \perp a, and the corollary of Section 5.1 on cancelling a coprime factor gives p∣bp \mid b. The general form follows by induction on kk, with a=a1⋯ak−1a = a_1 \cdots a_{k-1} and b=akb = a_k. □\square

Theorem P2.5.31 (unique factorization). Each integer n≥1n \ge 1 is a product of primes, n=p1p2⋯pk,p1≤p2≤⋯≤pk,n = p_1 p_2 \cdots p_k, \qquad p_1 \le p_2 \le \cdots \le p_k, and the list p1,…,pkp_1, \dots, p_k is the same for each such product. The case n=1n = 1 is the empty product, with k=0k = 0.

Lean: Nat.prod_primeFactorsList and Nat.primeFactorsList_unique in Mathlib.

Proof. See the Proof part. □\square

Group the equal primes: n=∏ppepn = \prod_p p^{e_p}, where the product runs over all primes and only finitely many exponents epe_p are not 00. This is the canonical form of nn.

Definition P2.5.32 (exponent of a prime). For n≥1n \ge 1, the exponent νp(n)\nu_p(n) is the exponent epe_p of pp in the canonical form of nn. Equally, νp(n)\nu_p(n) is the largest ee with pe∣np^e \mid n.

Example P2.5.33. 360=23⋅32⋅5360 = 2^3 \cdot 3^2 \cdot 5, so ν2(360)=3\nu_2(360) = 3, ν3(360)=2\nu_3(360) = 2, ν5(360)=1\nu_5(360) = 1 and νp(360)=0\nu_p(360) = 0 for each other prime pp.

Example P2.5.34. νp(mn)=νp(m)+νp(n)\nu_p(mn) = \nu_p(m) + \nu_p(n), since the canonical form of mnmn is the product of those of mm and nn. For instance 5040=7!=24⋅32⋅5⋅75040 = 7! = 2^4 \cdot 3^2 \cdot 5 \cdot 7, and ν2(5040)=ν2(2)+ν2(4)+ν2(6)=1+2+1=4\nu_2(5040) = \nu_2(2) + \nu_2(4) + \nu_2(6) = 1 + 2 + 1 = 4.

Corollary P2.5.35 (gcd and lcm by exponents). For m≥1m \ge 1 and n≥1n \ge 1 and each prime pp, νp(gcd⁡(m,n))=min⁡(νp(m),νp(n)),νp(lcm⁡(m,n))=max⁡(νp(m),νp(n)).\nu_p(\gcd(m, n)) = \min(\nu_p(m), \nu_p(n)), \qquad \nu_p(\operatorname{lcm}(m, n)) = \max(\nu_p(m), \nu_p(n)).

Lean: Nat.factorization_gcd and Nat.factorization_lcm in Mathlib.

Proof. By uniqueness, d∣md \mid m if and only if νp(d)≤νp(m)\nu_p(d) \le \nu_p(m) for each pp. So the common divisors of mm and nn are the dd with νp(d)≤min⁡(νp(m),νp(n))\nu_p(d) \le \min(\nu_p(m), \nu_p(n)) for each pp, and the largest of them has equality at each pp. The lcm is the same with ≥\ge and max⁡\max. □\square

Theorem P2.5.36 (infinitely many primes). For each nn there is a prime p>np > n.

Lean: Nat.exists_infinite_primes in Mathlib, in the form ∃p, n≤p∧p prime\exists p, \, n \le p \wedge p \text{ prime}.

Proof. Let N=n!+1N = n! + 1. By the first lemma, NN has a prime divisor pp. If p≤np \le n, then p∣n!p \mid n!, and so p∣N−n!=1p \mid N - n! = 1, which is false. So p>np > n. □\square

Theorem P2.5.37 (Legendre's formula). For each prime pp and each n≥0n \ge 0, νp(n!)=∑k≥1⌊npk⌋=⌊np⌋+⌊np2⌋+⌊np3⌋+⋯ .\nu_p(n!) = \sum_{k \ge 1} \left\lfloor \frac{n}{p^k} \right\rfloor = \left\lfloor \frac{n}{p} \right\rfloor + \left\lfloor \frac{n}{p^2} \right\rfloor + \left\lfloor \frac{n}{p^3} \right\rfloor + \cdots. The sum is finite: its terms are 00 once pk>np^k > n. This is eq. 1.2.5(8).

Lean: padicValNat_factorial in Mathlib, with the sum over 1≤k<b1 \le k < b for any bb with log⁡pn<b\log_p n < b.

Proof. See the Proof part. □\square

Corollary P2.5.38 (Legendre's formula by digits). Let sp(n)s_p(n) be the sum of the digits of nn in base pp. Then νp(n!)=n−sp(n)p−1.\nu_p(n!) = \frac{n - s_p(n)}{p - 1}.

Lean: Nat.Prime.sub_one_mul_multiplicity_factorial in Mathlib, in the form (p−1) νp(n!)=n−sp(n)(p - 1)\,\nu_p(n!) = n - s_p(n).

Proof. See the Proof part. □\square

Proof

Existence of the factorization. Induction on nn. For n=1n = 1 the empty product works. Let n≥2n \ge 2 and suppose each mm with 1≤m<n1 \le m < n is a product of primes. By the first lemma nn has a prime divisor pp. Then n=p⋅(n/p)n = p \cdot (n/p) with 1≤n/p<n1 \le n/p < n. The induction hypothesis writes n/pn/p as a product of primes, and the factor pp completes a product for nn. Sort the factors to get p1≤⋯≤pkp_1 \le \cdots \le p_k.

Uniqueness of the factorization. Suppose that some nn has two different sorted lists, and let nn be the least such number. Write n=p1p2⋯pk=q1q2⋯ql.n = p_1 p_2 \cdots p_k = q_1 q_2 \cdots q_l. Here n≥2n \ge 2, since 11 has only the empty list. So k≥1k \ge 1 and l≥1l \ge 1. The prime p1p_1 divides q1⋯qlq_1 \cdots q_l, so by Euclid's lemma p1∣qjp_1 \mid q_j for some jj. The only prime divisor of the prime qjq_j is qjq_j, so p1=qjp_1 = q_j. Cancel this prime from both products. The quotient n/p1n / p_1 has the two lists with one copy of p1p_1 removed. These two lists are different, since the full lists were different and the same prime left both. But n/p1<nn / p_1 < n, and nn was the least number with two lists. This is a contradiction. So no nn has two lists.

Note where the proof uses Euclid's lemma. That lemma came from Bézout's identity, so unique factorization rests on Euclid's algorithm. In some number systems other than the integers the lemma fails, and factorization into irreducible elements is not unique. A standard example is 6=2⋅3=(1+−5)(1−−5)6 = 2 \cdot 3 = (1 + \sqrt{-5})(1 - \sqrt{-5}) among the numbers a+b−5a + b\sqrt{-5}.

Legendre's formula. Write n!=1⋅2⋯nn! = 1 \cdot 2 \cdots n. Since νp\nu_p of a product is the sum of the values νp\nu_p of the factors, νp(n!)=∑j=1nνp(j).\nu_p(n!) = \sum_{j=1}^{n} \nu_p(j). Now count in a different order. The value νp(j)\nu_p(j) is the number of k≥1k \ge 1 with pk∣jp^k \mid j. So ∑j=1nνp(j)=∑j=1n∑k≥1[ pk∣j ]=∑k≥1∑j=1n[ pk∣j ].\sum_{j=1}^{n} \nu_p(j) = \sum_{j=1}^{n} \sum_{k \ge 1} [\, p^k \mid j \,] = \sum_{k \ge 1} \sum_{j=1}^{n} [\, p^k \mid j \,]. The inner sum counts the multiples of pkp^k in {1,…,n}\{1, \dots, n\}. These are pk,2pk,…,⌊n/pk⌋pkp^k, 2p^k, \dots, \lfloor n/p^k \rfloor p^k, so there are ⌊n/pk⌋\lfloor n / p^k \rfloor of them. This gives the formula. The exchange of the two sums is allowed, since only finitely many terms are not 00 (Chapter 1 gives the rules for exchanging sums).

Legendre's formula by digits. Write n=drpr+⋯+d1p+d0n = d_r p^r + \cdots + d_1 p + d_0 in base pp, with digits 0≤di<p0 \le d_i < p. Then ⌊n/pk⌋=drpr−k+⋯+dk+1p+dk\lfloor n / p^k \rfloor = d_r p^{r-k} + \cdots + d_{k+1} p + d_k for 1≤k≤r1 \le k \le r, since the rest dk−1pk−1+⋯+d0d_{k-1} p^{k-1} + \cdots + d_0 is less than pkp^k. Add over kk. The digit did_i appears in the terms k=1,…,ik = 1, \dots, i, with weights pi−1,pi−2,…,1p^{i-1}, p^{i-2}, \dots, 1. Their sum is pi−1+⋯+p+1=pi−1p−1.p^{i-1} + \cdots + p + 1 = \frac{p^i - 1}{p - 1}. So νp(n!)=∑i=0rdipi−1p−1=∑idipi−∑idip−1=n−sp(n)p−1.\nu_p(n!) = \sum_{i=0}^{r} d_i \frac{p^i - 1}{p - 1} = \frac{\sum_i d_i p^i - \sum_i d_i}{p - 1} = \frac{n - s_p(n)}{p - 1}.

Examples

Example P2.5.39. The exponents of the small primes in 100!100!. For p=2p = 2 the terms are ⌊100/2⌋+⌊100/4⌋+⌊100/8⌋+⌊100/16⌋+⌊100/32⌋+⌊100/64⌋=50+25+12+6+3+1=97.\lfloor 100/2 \rfloor + \lfloor 100/4 \rfloor + \lfloor 100/8 \rfloor + \lfloor 100/16 \rfloor + \lfloor 100/32 \rfloor + \lfloor 100/64 \rfloor = 50 + 25 + 12 + 6 + 3 + 1 = 97. For p=3p = 3: 33+11+3+1=4833 + 11 + 3 + 1 = 48. For p=5p = 5: 20+4=2420 + 4 = 24. For p=7p = 7: 14+2=1614 + 2 = 16. The digit form checks the first value: 100=11001002100 = 1100100_2 has three 11 digits, and (100−3)/1=97(100 - 3)/1 = 97. It also checks the value for p=3p = 3: 100=102013100 = 10201_3 has digit sum 44, and (100−4)/2=48(100 - 4)/2 = 48.

Example P2.5.40. The number of zeros at the end of n!n! in decimal is min⁡(ν2(n!),ν5(n!))\min(\nu_2(n!), \nu_5(n!)), since 10=2⋅510 = 2 \cdot 5. The exponent of 55 is the smaller one, since each term ⌊n/5k⌋\lfloor n/5^k \rfloor is at most the term ⌊n/2k⌋\lfloor n/2^k \rfloor. So 100!100! ends in 2424 zeros, and 1000!1000! ends in 200+40+8+1=249200 + 40 + 8 + 1 = 249 zeros.

Example P2.5.41. The proof of the infinity of primes does not claim that n!+1n! + 1 is prime. For n=5n = 5, 5!+1=121=1125! + 1 = 121 = 11^2: the new prime is 1111. For n=4n = 4, 4!+1=25=524! + 1 = 25 = 5^2. A similar product, 2⋅3⋅5⋅7⋅11⋅13+1=300312 \cdot 3 \cdot 5 \cdot 7 \cdot 11 \cdot 13 + 1 = 30031, is 59⋅50959 \cdot 509, and both factors exceed 1313, as the argument says they must.

Example P2.5.42. The gcd and the lcm by exponents. Take m=360=23⋅32⋅5m = 360 = 2^3 \cdot 3^2 \cdot 5 and n=1001⋅6=6006=2⋅3⋅7⋅11⋅13n = 1001 \cdot 6 = 6006 = 2 \cdot 3 \cdot 7 \cdot 11 \cdot 13. Then gcd⁡(360,6006)=2⋅3=6,lcm⁡(360,6006)=23⋅32⋅5⋅7⋅11⋅13=360360.\gcd(360, 6006) = 2 \cdot 3 = 6, \qquad \operatorname{lcm}(360, 6006) = 2^3 \cdot 3^2 \cdot 5 \cdot 7 \cdot 11 \cdot 13 = 360360. The check of Section 5.1 holds: 6⋅360360=2162160=360⋅60066 \cdot 360360 = 2162160 = 360 \cdot 6006.

Example P2.5.43. The canonical form gives the number of divisors. A positive divisor of n=∏pepn = \prod p^{e_p} is ∏pfp\prod p^{f_p} with 0≤fp≤ep0 \le f_p \le e_p, and each choice of the fpf_p gives a different divisor by uniqueness. So nn has ∏p(ep+1)\prod_p (e_p + 1) positive divisors. For 360360 that is (3+1)(2+1)(1+1)=24(3+1)(2+1)(1+1) = 24.

Exercises

  1. [10] Factor 99919991 and 1000110001 into primes.
  2. [15] Compute N=2⋅3⋅5⋅7⋅11⋅13+1N = 2 \cdot 3 \cdot 5 \cdot 7 \cdot 11 \cdot 13 + 1 and its prime factors. Explain without the factors why no prime p≤13p \le 13 divides NN.
  3. [M20] Prove that n4+64n^4 + 64 is composite for each integer n≥1n \ge 1.
  4. [12] Compute ν2(50!)\nu_2(50!), ν3(50!)\nu_3(50!), ν5(50!)\nu_5(50!) and ν7(50!)\nu_7(50!). How many zeros are at the end of 50!50! in decimal?
  5. [M20] How many zeros are at the end of 2024!2024! when it is written in base 1212?
  6. [M20] Prove that ν2(n!)=n−s2(n)\nu_2(n!) = n - s_2(n), where s2(n)s_2(n) is the number of 11 digits of nn in binary. Find all n≥1n \ge 1 with ν2(n!)=n−1\nu_2(n!) = n - 1.
  7. [M25] Let n≥1n \ge 1, and let pep^e be the largest power of the prime pp that divides (2nn)=(2n)!/(n!)2\binom{2n}{n} = (2n)!/(n!)^2. Prove that pe≤2np^e \le 2n.
  8. [M22] Prove that an integer n≥1n \ge 1 is a perfect square if and only if it has an odd number of positive divisors.

Lean

Mathlib has primes as Nat.Prime, the least prime divisor as Nat.minFac, with Nat.minFac_prime, and the existence of a prime divisor as Nat.exists_prime_and_dvd. Euclid's lemma is Nat.Prime.dvd_mul. The factorization is the list Nat.primeFactorsList n, sorted, with Nat.prod_primeFactorsList for existence and Nat.primeFactorsList_unique for uniqueness. The exponents form the finitely supported function Nat.factorization n, so νp(n)\nu_p(n) is n.factorization p. The gcd and lcm rules are Nat.factorization_gcd and Nat.factorization_lcm. The infinity of primes is Nat.exists_infinite_primes.

Legendre's formula is padicValNat_factorial in the file of pp-adic valuations. It states νp(n!)=∑1≤i<b⌊n/pi⌋\nu_p(n!) = \sum_{1 \le i < b} \lfloor n / p^i \rfloor for each bound b>log⁡pnb > \log_p n, so the finite sum of the text is one instance. The digit form is Nat.Prime.sub_one_mul_multiplicity_factorial.

Notes and history

Euclid proved the infinity of primes in Book IX of the Elements, with the product of a finite list of primes plus one. The existence of the factorization is also in the Elements, but a clear statement and proof of its uniqueness first appear in Gauss's Disquisitiones Arithmeticae of 1801. Legendre gave the formula for νp(n!)\nu_p(n!) in his Essai sur la théorie des nombres of 1808. TAOCP states it as eq. 1.2.5(8), and several exercises of Section 1.2.5 build on it. Exercise 5.2-7 is a first step to the exponent of a prime in a binomial coefficient, a question that the exercises of Section 1.2.6 of TAOCP take further.

Section 4.5.4 of TAOCP studies the factoring of large numbers and the tests for primality. The trial division of the second example of a prime is its first and slowest method. The distribution of primes, the prime number theorem and the sieve of Eratosthenes are topics of P6.

Congruences

Statement

A congruence is an equation that holds up to a multiple of a fixed modulus. Section 1.2.4 of TAOCP defines it by eq. 1.2.4(5) and states four laws, A to D. This section proves the four laws, finds inverses, solves linear congruences and proves the Chinese remainder theorem. The modulus mm is a positive integer.

Definition P2.5.44 (congruence). Let m≥1m \ge 1. Two integers aa and bb are congruent modulo mm when m∣a−bm \mid a - b. The notation is a≡b(modm)a \equiv b \pmod{m}. The number mm is the modulus.

Example P2.5.45. 38≡3(mod7)38 \equiv 3 \pmod{7}, since 38−3=35=5⋅738 - 3 = 35 = 5 \cdot 7. Also −4≡3(mod7)-4 \equiv 3 \pmod{7}, since −4−3=−7-4 - 3 = -7. But 38≢4(mod7)38 \not\equiv 4 \pmod{7}, since 3434 is not a multiple of 77.

Example P2.5.46. Modulo 22, the integers fall into two classes: a≡0a \equiv 0 for even aa and a≡1a \equiv 1 for odd aa. Modulo 11, all integers are congruent, since 11 divides each difference. A clock shows the hour modulo 1212: 99 hours after 77 o'clock it is 44 o'clock, since 16≡4(mod12)16 \equiv 4 \pmod{12}.

Lemma P2.5.47 (two forms of the definition). a≡b(modm)a \equiv b \pmod{m} if and only if a mod m=b mod ma \bmod m = b \bmod m. In particular a≡a mod m(modm)a \equiv a \bmod m \pmod{m}, and each integer is congruent to exactly one of 0,1,…,m−10, 1, \dots, m - 1.

Lean: Nat.ModEq, defined by a mod n=b mod na \bmod n = b \bmod n, and Nat.modEq_iff_dvd in Mathlib.

Proof. Write a=qm+ra = qm + r and b=q′m+r′b = q'm + r' with remainders r=a mod mr = a \bmod m and r′=b mod mr' = b \bmod m in [0,m)[0, m). Then a−b=(q−q′)m+(r−r′)a - b = (q - q')m + (r - r'). So m∣a−bm \mid a - b if and only if m∣r−r′m \mid r - r'. Since ∣r−r′∣<m|r - r'| < m, this holds if and only if r=r′r = r'. □\square

The second form is the definition of eq. 1.2.4(5). TAOCP states it for real aa, bb and mm, with the  mod \bmod of Section 4.3. For integers the two forms agree, by the lemma. The rest of this chapter uses integers only.

Theorem P2.5.48 (an equivalence relation). Congruence modulo mm is reflexive, symmetric and transitive: a≡aa \equiv a; if a≡ba \equiv b, then b≡ab \equiv a; if a≡ba \equiv b and b≡cb \equiv c, then a≡ca \equiv c, all modulo mm.

Lean: Nat.ModEq.refl, Nat.ModEq.symm and Nat.ModEq.trans in Mathlib.

Proof. m∣0m \mid 0; if m∣a−bm \mid a - b, then m∣b−am \mid b - a; if m∣a−bm \mid a - b and m∣b−cm \mid b - c, then m∣(a−b)+(b−c)=a−cm \mid (a - b) + (b - c) = a - c. □\square

The classes of the relation are the residue classes modulo mm. By the lemma there are exactly mm of them, one for each remainder.

Theorem P2.5.49 (Law A: addition and multiplication). If a≡ba \equiv b and x≡y(modm)x \equiv y \pmod{m}, then a+x≡b+y,a−x≡b−y,ax≡by(modm).a + x \equiv b + y, \qquad a - x \equiv b - y, \qquad ax \equiv by \pmod{m}. Hence an≡bn(modm)a^n \equiv b^n \pmod{m} for each n≥0n \ge 0.

Lean: Nat.ModEq.add, Nat.ModEq.mul and Nat.ModEq.pow in Mathlib; Int.ModEq.sub for the difference.

Proof. Let a−b=kma - b = km and x−y=lmx - y = lm. Then (a±x)−(b±y)=(k±l)m(a \pm x) - (b \pm y) = (k \pm l)m. For the product, ax−by=a(x−y)+(a−b)y=(al+ky)m.ax - by = a(x - y) + (a - b)y = (al + ky)m. The rule for powers follows by induction on nn from the rule for products. □\square

Theorem P2.5.50 (Law B: cancellation). If ax≡byax \equiv by and a≡b(modm)a \equiv b \pmod{m}, and if a⊥ma \perp m, then x≡y(modm)x \equiv y \pmod{m}.

Lean: Nat.ModEq.cancel_left_div_gcd in Mathlib, in the general form: from ca≡cb(modm)ca \equiv cb \pmod{m} it gives a≡b(modm/gcd⁡(m,c))a \equiv b \pmod{m / \gcd(m, c)}.

Proof. By Law A, by≡ayby \equiv ay, so ax≡ayax \equiv ay, that is m∣a(x−y)m \mid a(x - y). Since a⊥ma \perp m, the corollary of Section 5.1 on cancelling a coprime factor gives m∣x−ym \mid x - y. □\square

Theorem P2.5.51 (Law C: scaling the modulus). Let n≠0n \ne 0. Then a≡b(modm)a \equiv b \pmod{m} if and only if an≡bn(modm∣n∣)an \equiv bn \pmod{m|n|}.

Lean: Nat.ModEq.mul_left_cancel_iff' in Mathlib.

Proof. m∣a−bm \mid a - b if and only if m∣n∣∣(a−b)∣n∣m|n| \mid (a - b)|n|, since n≠0n \ne 0 can be cancelled in (a−b)∣n∣=km∣n∣(a - b)|n| = k m |n|. And (a−b)∣n∣=±(an−bn)(a - b)|n| = \pm(an - bn). □\square

TAOCP states Law C with the modulus mnmn for each n≠0n \ne 0, since its congruence allows a negative modulus. This chapter keeps the modulus positive, so it writes m∣n∣m|n|.

Theorem P2.5.52 (Law D: splitting the modulus). Let r⊥sr \perp s. Then a≡b(modrs)a \equiv b \pmod{rs} if and only if a≡b(modr)a \equiv b \pmod{r} and a≡b(mods)a \equiv b \pmod{s}.

Lean: Nat.modEq_and_modEq_iff_modEq_mul in Mathlib.

Proof. If rs∣a−brs \mid a - b, then r∣a−br \mid a - b and s∣a−bs \mid a - b. For the other direction, let a−b=kra - b = kr with s∣krs \mid kr. Since s⊥rs \perp r, the corollary of Section 5.1 gives s∣ks \mid k, so rs∣kr=a−brs \mid kr = a - b. □\square

Definition P2.5.53 (inverse modulo m). An inverse of aa modulo mm is an integer a′a' with aa′≡1(modm)a a' \equiv 1 \pmod{m}.

Example P2.5.54. Modulo 77, the inverse of 33 is 55, since 3⋅5=15=2⋅7+13 \cdot 5 = 15 = 2 \cdot 7 + 1. The inverses of 1,2,3,4,5,61, 2, 3, 4, 5, 6 modulo 77 are 1,4,5,2,3,61, 4, 5, 2, 3, 6. Each nonzero class modulo 77 has an inverse.

Example P2.5.55. Modulo 1212, the number 44 has no inverse. The products 4a4a are 0,4,8,12,16,…0, 4, 8, 12, 16, \dots, and modulo 1212 they are always 00, 44 or 88, never 11. The classes with inverses modulo 1212 are 1,5,7,111, 5, 7, 11, and each is its own inverse: 52=255^2 = 25, 72=497^2 = 49, 112=12111^2 = 121 are all ≡1\equiv 1.

Theorem P2.5.56 (existence of inverses). The integer aa has an inverse modulo mm if and only if a⊥ma \perp m. The inverse is unique modulo mm, and the extended algorithm of Section 5.1 computes it.

Lean: ZMod.coe_mul_inv_eq_one and ZMod.unitOfCoprime in Mathlib.

Proof. If a⊥ma \perp m, Bézout gives aa′+mb=1a a' + m b = 1, so aa′≡1a a' \equiv 1. If aa′≡1a a' \equiv 1, then aa′+mb=1a a' + m b = 1 for some bb, and each common divisor of aa and mm divides 11. If aa′≡1≡aa′′a a' \equiv 1 \equiv a a'', then Law B, with a⊥ma \perp m, gives a′≡a′′a' \equiv a''. □\square

Theorem P2.5.57 (linear congruences). Let d=gcd⁡(a,m)d = \gcd(a, m). The congruence ax≡b(modm)ax \equiv b \pmod{m} has a solution if and only if d∣bd \mid b. In that case it has exactly dd solutions modulo mm. They are x≡x0+kmd(modm),k=0,1,…,d−1,x \equiv x_0 + k \frac{m}{d} \pmod{m}, \qquad k = 0, 1, \dots, d - 1, where x0x_0 is any one solution.

Lean: not yet stated as one theorem; the parts are Nat.ModEq.cancel_left_div_gcd and ZMod.coe_mul_inv_eq_one.

Proof. See the Proof part. □\square

Theorem P2.5.58 (Chinese remainder theorem). Let m1,…,mkm_1, \dots, m_k be positive and pairwise coprime, and let M=m1m2⋯mkM = m_1 m_2 \cdots m_k. For all integers a1,…,aka_1, \dots, a_k, the system x≡a1(modm1),…,x≡ak(modmk)x \equiv a_1 \pmod{m_1}, \quad \dots, \quad x \equiv a_k \pmod{m_k} has a solution, and the solution is unique modulo MM.

Lean: Nat.chineseRemainder for two moduli and Nat.chineseRemainderOfList for a list in Mathlib; ZMod.chineseRemainder states the ring isomorphism ℤ/mn≅ℤ/m×ℤ/n\mathbb{Z}/mn \cong \mathbb{Z}/m \times \mathbb{Z}/n.

Proof. See the Proof part. □\square

Proof

Linear congruences. First let d∣bd \mid b. Write a=da1a = d a_1, b=db1b = d b_1 and m=dm1m = d m_1. Then a1⊥m1a_1 \perp m_1, as in the proof of the gcd times the lcm in Section 5.1. By Law C with n=dn = d, ax≡b(modm)  ⟺  a1x≡b1(modm1).ax \equiv b \pmod{m} \iff a_1 x \equiv b_1 \pmod{m_1}. Since a1⊥m1a_1 \perp m_1, the number a1a_1 has an inverse a1′a_1' modulo m1m_1. Multiply by it (Law A), and in the other direction multiply by a1a_1: the congruence holds if and only if x≡a1′b1(modm1)x \equiv a_1' b_1 \pmod{m_1}. So the solutions form one class x0+m1ℤx_0 + m_1 \mathbb{Z} modulo m1m_1. Modulo m=dm1m = d m_1 this class splits into the dd classes x0+km1x_0 + k m_1 for 0≤k<d0 \le k < d. They are different modulo mm, since the differences (k−k′)m1(k - k') m_1 with 0<∣k−k′∣<d0 < |k - k'| < d are not multiples of dm1d m_1.

Now let d∤bd \nmid b. If ax≡bax \equiv b, then b=ax−lmb = ax - lm for some ll, and dd divides the right side. So no solution exists.

The Chinese remainder theorem. Uniqueness. If xx and yy both solve the system, then x≡y(modmj)x \equiv y \pmod{m_j} for each jj. Law D, applied k−1k - 1 times, joins these congruences to x≡y(modM)x \equiv y \pmod{M}. Each step is allowed: m1⋯mj−1m_1 \cdots m_{j-1} is coprime to mjm_j, since a prime divisor of the product divides one of the factors mim_i by Euclid's lemma, and mi⊥mjm_i \perp m_j.

Existence. Put Mj=M/mjM_j = M / m_j. Then Mj⊥mjM_j \perp m_j, by the same argument. Let Mj′M_j' be an inverse of MjM_j modulo mjm_j. Put x=a1M1M1′+a2M2M2′+⋯+akMkMk′.x = a_1 M_1 M_1' + a_2 M_2 M_2' + \cdots + a_k M_k M_k'. Modulo mjm_j, each term with i≠ji \ne j is ≡0\equiv 0, since mj∣Mim_j \mid M_i. The term with i=ji = j is ≡aj⋅1\equiv a_j \cdot 1. So x≡aj(modmj)x \equiv a_j \pmod{m_j} for each jj.

There is also a short proof by counting. The map from the classes modulo MM to the lists of classes modulo m1,…,mkm_1, \dots, m_k is one to one, by uniqueness. Both sets have MM elements. A one to one map between two finite sets of the same size is onto, so each list of classes is reached. This proof gives no formula for xx.

Examples

Example P2.5.59. The inverse of 2323 modulo 3737. The extended algorithm in Section 5.1 gave 5⋅1517−8⋅943=415 \cdot 1517 - 8 \cdot 943 = 41. Divide by 4141: 5⋅37−8⋅23=15 \cdot 37 - 8 \cdot 23 = 1. So 23⋅(−8)≡1(mod37)23 \cdot (-8) \equiv 1 \pmod{37}, and the inverse is −8≡29-8 \equiv 29. Check: 23⋅29=667=18⋅37+123 \cdot 29 = 667 = 18 \cdot 37 + 1.

Example P2.5.60. Solve 35x≡14(mod91)35x \equiv 14 \pmod{91}. Here d=gcd⁡(35,91)=7d = \gcd(35, 91) = 7 and 7∣147 \mid 14, so there are 77 solutions modulo 9191. Divide by 77: 5x≡2(mod13)5x \equiv 2 \pmod{13}. The inverse of 55 modulo 1313 is 88, since 40=3⋅13+140 = 3 \cdot 13 + 1. So x≡16≡3(mod13)x \equiv 16 \equiv 3 \pmod{13}. The solutions modulo 9191 are x≡3,16,29,42,55,68,81(mod91).x \equiv 3, 16, 29, 42, 55, 68, 81 \pmod{91}. Check one: 35⋅3=105=91+1435 \cdot 3 = 105 = 91 + 14.

Example P2.5.61. Solve x≡2(mod5)x \equiv 2 \pmod{5}, x≡3(mod7)x \equiv 3 \pmod{7}, x≡4(mod9)x \equiv 4 \pmod{9}. The moduli are pairwise coprime, and M=315M = 315. Then M1=63M_1 = 63, M2=45M_2 = 45, M3=35M_3 = 35. The inverses: 63≡3(mod5)63 \equiv 3 \pmod{5} has inverse 22; 45≡3(mod7)45 \equiv 3 \pmod{7} has inverse 55; 35≡8(mod9)35 \equiv 8 \pmod{9} has inverse 88. So x≡2⋅63⋅2+3⋅45⋅5+4⋅35⋅8=252+675+1120=2047≡157(mod315).x \equiv 2 \cdot 63 \cdot 2 + 3 \cdot 45 \cdot 5 + 4 \cdot 35 \cdot 8 = 252 + 675 + 1120 = 2047 \equiv 157 \pmod{315}. Check: 157=31⋅5+2=22⋅7+3=17⋅9+4157 = 31 \cdot 5 + 2 = 22 \cdot 7 + 3 = 17 \cdot 9 + 4.

Example P2.5.62. Casting out nines. Since 10≡1(mod9)10 \equiv 1 \pmod{9}, Law A gives 10k≡110^k \equiv 1, and a number is congruent modulo 99 to the sum of its decimal digits. So 123456789≡45≡0(mod9)123456789 \equiv 45 \equiv 0 \pmod{9}. In the same way 10≡−1(mod11)10 \equiv -1 \pmod{11}, and a number is congruent modulo 1111 to the alternating sum of its digits, starting with the units digit: 918082≡2−8+0−8+1−9=−22≡0(mod11)918082 \equiv 2 - 8 + 0 - 8 + 1 - 9 = -22 \equiv 0 \pmod{11}, and indeed 918082=11⋅83462918082 = 11 \cdot 83462.

Example P2.5.63. Cancellation without the coprime condition. From 6x≡6y(mod9)6x \equiv 6y \pmod{9} one may not cancel 66 to get x≡y(mod9)x \equiv y \pmod{9}: for x=3x = 3 and y=0y = 0, both sides are 18≡018 \equiv 0 and 00. Law C with n=3n = 3 turns 6x≡6y(mod9)6x \equiv 6y \pmod{9} into 2x≡2y(mod3)2x \equiv 2y \pmod{3}, and Law B with 2⊥32 \perp 3 then gives x≡y(mod3)x \equiv y \pmod{3}. That is the general rule of the Lean name above: the modulus 99 becomes 9/gcd⁡(9,6)=39 / \gcd(9, 6) = 3.

Exercises

  1. [10] Decide, with a reason, whether 1000≡−1(mod7)1000 \equiv -1 \pmod{7} and whether 210≡1(mod11)2^{10} \equiv 1 \pmod{11}.
  2. [M15] Prove that p2≡1(mod24)p^2 \equiv 1 \pmod{24} for each prime p>3p > 3.
  3. [15] Find the inverse of 1717 modulo 6060 with the extended algorithm.
  4. [20] Find all solutions of 91x≡35(mod154)91x \equiv 35 \pmod{154}.
  5. [20] Find the least positive xx with x≡1(mod4)x \equiv 1 \pmod{4}, x≡2(mod9)x \equiv 2 \pmod{9} and x≡3(mod25)x \equiv 3 \pmod{25}.
  6. [M18] Since 1001=7⋅11⋅131001 = 7 \cdot 11 \cdot 13, show that a number is congruent modulo 10011001 to the alternating sum of its blocks of three decimal digits, from the right. Use this to find 123456789123456789 modulo 77, 1111 and 1313.
  7. [M22] Prove that x2≡1(modp)x^2 \equiv 1 \pmod{p} has only the solutions x≡±1x \equiv \pm 1 for a prime pp. Then find all solutions of x2≡1(mod105)x^2 \equiv 1 \pmod{105}.
  8. [M24] Prove that a≡b(modr)a \equiv b \pmod{r} and a≡b(mods)a \equiv b \pmod{s} hold together if and only if a≡b(modlcm⁡(r,s))a \equiv b \pmod{\operatorname{lcm}(r, s)}. Show that Law D is a special case.

Lean

Mathlib writes a≡b(modn)a \equiv b \pmod{n} as a ≡ b [MOD n] for natural numbers (Nat.ModEq) and as a ≡ b [ZMOD n] for integers (Int.ModEq). The definition is the second form of the lemma, equal remainders, and Nat.modEq_iff_dvd gives the first form. The laws are Nat.ModEq.add, Nat.ModEq.mul, Nat.ModEq.pow (Law A), Nat.ModEq.cancel_left_div_gcd (Law B, in a stronger form), Nat.ModEq.mul_left_cancel_iff' (Law C) and Nat.modEq_and_modEq_iff_modEq_mul (Law D).

The other view is the ring ZMod n of residue classes. In it a congruence is an equation, and an inverse is an inverse in the ring: ZMod.coe_mul_inv_eq_one says that x⋅x−1=1x \cdot x^{-1} = 1 in ZMod n when x⊥nx \perp n. The Chinese remainder theorem is Nat.chineseRemainder (a solution with its two congruences), Nat.chineseRemainder_lt_mul (the solution below mnmn) and ZMod.chineseRemainder (the ring isomorphism). TAOCP's congruence for real numbers, eq. 1.2.4(5), compares values of TAOCP.kmod in TAOCP/Notation (tamnd/taocp-lean main); for an integer and a natural modulus, TAOCP.kmod_intCast_natCast reduces it to Int.emod and so to Int.ModEq.

Notes and history

Gauss introduced the sign ≡\equiv and the word "modulus" in the Disquisitiones Arithmeticae of 1801. The Chinese remainder theorem is older by far: a problem of the kind of the third example is in the Sunzi Suanjing, a Chinese text of about the fourth or fifth century. Qin Jiushao gave a general method in 1247.

TAOCP uses congruences in many places. The random number generators of Section 3.2.1 are defined by Xn+1=(aXn+c) mod mX_{n+1} = (aX_n + c) \bmod m, and their period depends on Laws A to D. Section 4.3.2 does arithmetic on large numbers by the Chinese remainder theorem: it keeps a number as its residues modulo several coprime moduli, and then it adds and multiplies each residue on its own. Section 4.5.2 computes inverses by the extended algorithm.

The theorems of Fermat, Euler and Wilson

Statement

This section proves three classical theorems about powers and products modulo mm. Fermat's theorem is Theorem 1.2.4F of TAOCP. Euler's theorem extends it to a modulus that is not prime, with Euler's function φ\varphi in place of p−1p - 1. Wilson's theorem gives (p−1)! mod p(p - 1)! \bmod p. All three rest on one idea: multiplication by a number coprime to mm permutes the residue classes that are coprime to mm.

Theorem P2.5.64 (Fermat's theorem). Let pp be prime. For each integer aa, ap≡a(modp).a^p \equiv a \pmod{p}. If p∤ap \nmid a, then also ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}.

Lean: ZMod.pow_card (the form xp=xx^p = x in ZMod p) and Int.ModEq.pow_card_sub_one_eq_one in Mathlib.

Proof. The Proof part gives two proofs. The first is the proof of TAOCP, by eqs. 1.2.4(6) to 1.2.4(8). The second is different: it counts strings of letters. □\square

Definition P2.5.65 (Euler's function). For n≥1n \ge 1, φ(n)\varphi(n) is the number of integers kk with 1≤k≤n1 \le k \le n and k⊥nk \perp n.

Example P2.5.66. The integers in {1,…,12}\{1, \dots, 12\} that are coprime to 1212 are 1,5,7,111, 5, 7, 11. So φ(12)=4\varphi(12) = 4. For a prime pp, each of 1,…,p−11, \dots, p - 1 is coprime to pp, and pp is not, so φ(p)=p−1\varphi(p) = p - 1.

Example P2.5.67. The first values are φ(1),…,φ(12)=1,1,2,2,4,2,6,4,6,4,10,4.\varphi(1), \dots, \varphi(12) = 1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4. Here φ(1)=1\varphi(1) = 1, because gcd⁡(1,1)=1\gcd(1, 1) = 1. The values are even from n=3n = 3 on, since k⊥nk \perp n exactly when n−k⊥nn - k \perp n, and the two numbers kk and n−kn - k are different unless n=2kn = 2k, a case with gcd⁡(k,n)=k>1\gcd(k, n) = k > 1 for n>2n > 2.

Lemma P2.5.68 (prime powers). For a prime pp and e≥1e \ge 1, φ(pe)=pe−pe−1=pe−1(p−1)\varphi(p^e) = p^e - p^{e-1} = p^{e-1}(p - 1).

Lean: Nat.totient_prime_pow and Nat.totient_prime in Mathlib.

Proof. A number kk fails to be coprime to pep^e exactly when p∣kp \mid k. In {1,…,pe}\{1, \dots, p^e\} there are pe−1p^{e-1} multiples of pp. □\square

Theorem P2.5.69 (multiplicativity of φ\varphi). If m⊥nm \perp n, then φ(mn)=φ(m)φ(n)\varphi(mn) = \varphi(m)\varphi(n). Hence φ(n)=∏p∣npep−1(p−1)=n∏p∣n(1−1p),n=∏ppep.\varphi(n) = \prod_{p \mid n} p^{e_p - 1}(p - 1) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right), \qquad n = \prod_p p^{e_p}.

Lean: Nat.totient_mul and Nat.totient_eq_prod_factorization in Mathlib.

Proof. See the Proof part. □\square

Theorem P2.5.70 (the divisor sum of φ\varphi). For each n≥1n \ge 1, ∑d∣nφ(d)=n,\sum_{d \mid n} \varphi(d) = n, where the sum runs over the positive divisors dd of nn.

Lean: Nat.sum_totient in Mathlib.

Proof. See the Proof part. □\square

Theorem P2.5.71 (Euler's theorem). If a⊥ma \perp m, then aφ(m)≡1(modm)a^{\varphi(m)} \equiv 1 \pmod{m}.

Lean: Nat.ModEq.pow_totient and ZMod.pow_totient in Mathlib.

Proof. See the Proof part. □\square

Theorem P2.5.72 (Wilson's theorem). An integer n≥2n \ge 2 is prime if and only if (n−1)!≡−1(modn)(n - 1)! \equiv -1 \pmod{n}.

Lean: ZMod.wilsons_lemma (one direction) and Nat.prime_iff_fac_equiv_neg_one (both) in Mathlib.

Proof. See the Proof part. □\square

Proof

Fermat, first proof. This is the proof of Section 1.2.4. Let p∤ap \nmid a. Consider the p−1p - 1 numbers a mod p,2a mod p,…,(p−1)a mod p.a \bmod p, \quad 2a \bmod p, \quad \dots, \quad (p - 1)a \bmod p. None of them is 00: p∣kap \mid ka with p∤ap \nmid a would give p∣kp \mid k by Euclid's lemma, but 0<k<p0 < k < p. No two are equal: ka≡laka \equiv la gives k≡lk \equiv l by Law B, since a⊥pa \perp p, and two numbers in {1,…,p−1}\{1, \dots, p - 1\} that are congruent modulo pp are equal. So the list holds p−1p - 1 different values in {1,…,p−1}\{1, \dots, p - 1\}. It is a permutation of 1,2,…,p−11, 2, \dots, p - 1. Multiply the list (Law A): a⋅2a⋯(p−1)a≡1⋅2⋯(p−1)(modp),that isap−1(p−1)!≡(p−1)!(modp).a \cdot 2a \cdots (p - 1)a \equiv 1 \cdot 2 \cdots (p - 1) \pmod{p}, \qquad \text{that is} \qquad a^{p-1}(p - 1)! \equiv (p - 1)! \pmod{p}. The number (p−1)!(p - 1)! is coprime to pp, since no factor of it has the factor pp. Law B cancels it: ap−1≡1a^{p-1} \equiv 1. Multiply by aa to get ap≡aa^p \equiv a. If p∣ap \mid a, then both sides of ap≡aa^p \equiv a are ≡0\equiv 0.

Fermat, second proof. This proof is not the one in TAOCP. Let a≥1a \ge 1, and think of aa as the size of an alphabet. There are apa^p strings of length pp. Of these, aa strings are constant, such as xx⋯xxx \cdots x. Call two strings equivalent when one is a cyclic shift of the other. Let ss be a string that is not constant, and let dd be the least positive shift that maps ss to itself. The shift by pp maps ss to itself. The shifts that fix ss are closed under sums and differences, so by Bézout the shift by gcd⁡(d,p)\gcd(d, p) also fixes ss. Hence d∣pd \mid p. Since pp is prime, d=1d = 1 or d=pd = p. If d=1d = 1, then ss is constant. So d=pd = p, and the pp shifts of ss are all different. So the strings that are not constant fall into classes of exactly pp strings each, and p∣ap−a.p \mid a^p - a. This proves the theorem for a≥1a \ge 1. For a=0a = 0 it is clear. For a<0a < 0, use a≡a+kp(modp)a \equiv a + kp \pmod p with a+kp≥1a + kp \ge 1, and Law A.

Multiplicativity of φ\varphi. Let m⊥nm \perp n. By the Chinese remainder theorem of Section 5.3, the map x mod mn  ↦  (x mod m,  x mod n)x \bmod mn \;\mapsto\; (x \bmod m, \; x \bmod n) is a one to one correspondence between the mnmn classes modulo mnmn and the mnmn pairs of classes. A number xx is coprime to mnmn if and only if it is coprime to mm and to nn, since a prime divides mnmn only if it divides mm or nn. Also x⊥mx \perp m depends only on x mod mx \bmod m, since gcd⁡(x,m)=gcd⁡(x mod m,m)\gcd(x, m) = \gcd(x \bmod m, m). So the correspondence maps the classes coprime to mnmn onto the pairs of a class coprime to mm and a class coprime to nn. There are φ(m)φ(n)\varphi(m)\varphi(n) such pairs. The product formula follows from the lemma on prime powers: the prime powers pepp^{e_p} of nn are pairwise coprime.

The divisor sum. Sort the nn numbers k∈{1,…,n}k \in \{1, \dots, n\} by the value of gcd⁡(k,n)\gcd(k, n). For a divisor gg of nn, the numbers kk with gcd⁡(k,n)=g\gcd(k, n) = g are k=gjk = g j with 1≤j≤n/g1 \le j \le n/g and gcd⁡(j,n/g)=1\gcd(j, n/g) = 1, by the rule gcd⁡(gj,g⋅n/g)=ggcd⁡(j,n/g)\gcd(gj, g \cdot n/g) = g \gcd(j, n/g) (exercise 5.1-4). There are φ(n/g)\varphi(n/g) of them. So n=∑g∣nφ(n/g)n = \sum_{g \mid n} \varphi(n/g). As gg runs over the divisors of nn, so does d=n/gd = n/g.

Euler's theorem. Let r1,…,rφ(m)r_1, \dots, r_{\varphi(m)} be the numbers in {1,…,m}\{1, \dots, m\} that are coprime to mm. The numbers ari mod ma r_i \bmod m are coprime to mm, since aa and rir_i are. They are different, by Law B. So they are the rir_i in another order. Multiply them all: aφ(m)r1⋯rφ(m)≡r1⋯rφ(m)(modm).a^{\varphi(m)} r_1 \cdots r_{\varphi(m)} \equiv r_1 \cdots r_{\varphi(m)} \pmod{m}. The product of the rir_i is coprime to mm, and Law B cancels it. For a prime m=pm = p this is the first proof of Fermat again, since φ(p)=p−1\varphi(p) = p - 1.

Wilson's theorem. Let pp be prime. For p=2p = 2, 1!=1≡−11! = 1 \equiv -1. Let p≥3p \ge 3. Each aa in {1,…,p−1}\{1, \dots, p - 1\} has an inverse a′a' in the same set (Section 5.3). The inverse is aa itself exactly when a2≡1a^2 \equiv 1, that is p∣(a−1)(a+1)p \mid (a - 1)(a + 1). By Euclid's lemma this means a=1a = 1 or a=p−1a = p - 1. So the numbers 2,…,p−22, \dots, p - 2 fall into pairs {a,a′}\{a, a'\} with aa′≡1a a' \equiv 1, and their product is ≡1\equiv 1. Hence (p−1)!≡1⋅1⋅(p−1)≡−1(p - 1)! \equiv 1 \cdot 1 \cdot (p - 1) \equiv -1.

For the other direction, let n≥2n \ge 2 be composite, with a divisor dd and 1<d<n1 < d < n. Then d∣(n−1)!d \mid (n - 1)!, since d≤n−1d \le n - 1. If (n−1)!≡−1(modn)(n - 1)! \equiv -1 \pmod{n}, then d∣(n−1)!+1d \mid (n - 1)! + 1, and so d∣1d \mid 1, which is false. So the congruence fails for each composite nn.

Examples

Example P2.5.73. The permutation of the first proof for p=13p = 13 and a=5a = 5. The numbers 5k mod 135k \bmod 13 for k=1,…,12k = 1, \dots, 12 are 5,10,2,7,12,4,9,1,6,11,3,8,5, 10, 2, 7, 12, 4, 9, 1, 6, 11, 3, 8, each of 1,…,121, \dots, 12 once. So 512≡1(mod13)5^{12} \equiv 1 \pmod{13}, and indeed a12≡1(mod13)a^{12} \equiv 1 \pmod{13} for each aa from 11 to 1212.

Example P2.5.74. Powers with Fermat. To find 21000 mod 132^{1000} \bmod 13, use 212≡12^{12} \equiv 1 and 1000=83⋅12+41000 = 83 \cdot 12 + 4: 21000=(212)83⋅24≡16≡3(mod13)2^{1000} = (2^{12})^{83} \cdot 2^4 \equiv 16 \equiv 3 \pmod{13}. In general an≡an mod (p−1)(modp)a^n \equiv a^{n \bmod (p-1)} \pmod p for p∤ap \nmid a.

Example P2.5.75. Euler's function by the product formula. 360=23⋅32⋅5360 = 2^3 \cdot 3^2 \cdot 5, so φ(360)=360⋅12⋅23⋅45=96=(22⋅1)(3⋅2)(4).\varphi(360) = 360 \cdot \frac{1}{2} \cdot \frac{2}{3} \cdot \frac{4}{5} = 96 = (2^2 \cdot 1)(3 \cdot 2)(4). Then Euler's theorem gives the last three digits of a power. Since φ(1000)=1000⋅12⋅45=400\varphi(1000) = 1000 \cdot \frac12 \cdot \frac45 = 400 and 3⊥10003 \perp 1000, 32026≡326(mod1000)3^{2026} \equiv 3^{26} \pmod{1000}. The value of 326 mod 10003^{26} \bmod 1000 is 329329, so 320263^{2026} ends in the digits 329329.

Example P2.5.76. The divisor sum for n=12n = 12. Write the fractions k/12k/12 for k=1,…,12k = 1, \dots, 12 in lowest terms: 112,16,14,13,512,12,712,23,34,56,1112,11.\tfrac{1}{12}, \tfrac16, \tfrac14, \tfrac13, \tfrac{5}{12}, \tfrac12, \tfrac{7}{12}, \tfrac23, \tfrac34, \tfrac56, \tfrac{11}{12}, \tfrac11. The denominators are divisors of 1212, and the denominator dd appears with the φ(d)\varphi(d) numerators coprime to dd. The counts for d=1,2,3,4,6,12d = 1, 2, 3, 4, 6, 12 are 1,1,2,2,2,41, 1, 2, 2, 2, 4, and their sum is 1212. This is the proof of the theorem, seen on one case.

Example P2.5.77. Wilson's pairs for p=13p = 13. The inverses modulo 1313 pair the numbers 2,…,112, \dots, 11 as {2,7},  {3,9},  {4,10},  {5,8},  {6,11},\{2, 7\}, \; \{3, 9\}, \; \{4, 10\}, \; \{5, 8\}, \; \{6, 11\}, with products 14,27,40,40,6614, 27, 40, 40, 66, all ≡1(mod13)\equiv 1 \pmod{13}. So 12!≡1⋅12≡−1(mod13)12! \equiv 1 \cdot 12 \equiv -1 \pmod{13}. For p=11p = 11: 10!=3628800=329890⋅11+1010! = 3628800 = 329890 \cdot 11 + 10, and 10≡−110 \equiv -1.

Example P2.5.78. Fermat's theorem does not decide primality. The composite number 341=11⋅31341 = 11 \cdot 31 satisfies 2340≡1(mod341)2^{340} \equiv 1 \pmod{341}. But 3340≡56(mod341)3^{340} \equiv 56 \pmod{341}, so the base 33 shows that 341341 is not prime. Some composite numbers pass the test for each base coprime to them; exercise 5.4-4 gives one.

Exercises

  1. [10] Compute φ(1000)\varphi(1000), φ(1001)\varphi(1001) and φ(1024)\varphi(1024).
  2. [12] Find 31000 mod 73^{1000} \bmod 7 and 31000 mod 133^{1000} \bmod 13.
  3. [M18] Find the last two decimal digits of 720267^{2026}.
  4. [M20] Let n=1105=5⋅13⋅17n = 1105 = 5 \cdot 13 \cdot 17. Prove that a1104≡1(mod1105)a^{1104} \equiv 1 \pmod{1105} for each a⊥1105a \perp 1105, although 11051105 is not prime.
  5. [M20] Find all nn with φ(n)=12\varphi(n) = 12.
  6. [M22] Prove that (p−2)!≡1(modp)(p - 2)! \equiv 1 \pmod{p} for each prime pp. Then find 50! mod 5350! \bmod 53.
  7. [M23] Prove that a13≡a(mod2730)a^{13} \equiv a \pmod{2730} for each integer aa.
  8. [M24] Prove that for n≥2n \ge 2 the sum of the numbers kk with 1≤k≤n1 \le k \le n and k⊥nk \perp n is nφ(n)/2n\varphi(n)/2.

Lean

Mathlib defines Nat.totient n as the number of a<na < n with a⊥na \perp n, with the notation φ n. For n≥1n \ge 1 this agrees with the definition of the text, since 0⊥n0 \perp n only for n=1n = 1, and then the text counts k=1k = 1 in its place. The facts of the section are Nat.totient_prime, Nat.totient_prime_pow, Nat.totient_mul, Nat.totient_eq_prod_factorization and Nat.sum_totient; the last states ∑d∣nφ(d)=n\sum_{d \mid n} \varphi(d) = n as a sum over n.divisors.

Fermat's theorem is ZMod.pow_card in the ring ZMod p, and Int.ModEq.pow_card_sub_one_eq_one in the form ap−1≡1a^{p-1} \equiv 1. Euler's theorem is Nat.ModEq.pow_totient and ZMod.pow_totient. Wilson's theorem is ZMod.wilsons_lemma and its converse is part of Nat.prime_iff_fac_equiv_neg_one. Theorem 1.2.4F has no declaration of its own in tamnd/taocp-lean yet; it is an instance of ZMod.pow_card.

Notes and history

Fermat stated his theorem in a letter of 1640 without proof. Euler gave the first published proofs, in the 1730s and 1740s, and in 1763 he proved the general theorem with the function φ\varphi. The name φ\varphi is Gauss's. The second proof of the Proof part, by counting strings, is a standard proof of combinatorics. It needs no algebra beyond Bézout's identity, and it shows why the prime pp matters: a string of prime length has no period between 11 and pp. Wilson's theorem was announced by Edward Waring in 1770 as a result of his student John Wilson. Lagrange gave the first proof in 1771, together with the converse.

In TAOCP, Theorem 1.2.4F and its proof come with eqs. 1.2.4(6) to 1.2.4(8), and the first proof above follows that route. The theorem returns in Section 3.2.1.2 on the period of multiplicative random number generators, and in Section 4.5.4 as the base of the tests for primality. Section 4.6.3 computes large powers, and Euler's theorem lets such a computation modulo mm reduce the exponent modulo φ(m)\varphi(m).

The table below lists the results of TAOCP Section 1.2 that this chapter proves, with the numbers of the items of this chapter.

TAOCP 1.2 Result of P2
1.2.1, Algorithm E (extended Euclid) Algorithm P2.5.12, Theorem P2.5.11
1.2.4 eq. 5 Definition P2.5.44, Lemma P2.5.47
1.2.4 Law A Theorem P2.5.49
1.2.4 Law B Theorem P2.5.50
1.2.4 Law C Theorem P2.5.51
1.2.4 Law D Theorem P2.5.52
1.2.4 Theorem F and eqs. 6 to 8 Theorem P2.5.64
1.2.5 eq. 8 Theorem P2.5.37, Corollary P2.5.38
1.2.8 eqs. 7 to 9 Lemma P2.5.9, Algorithm P2.5.10

This chapter gives the number theory that Volume 1 uses. The first algorithm of TAOCP is Euclid's algorithm in Section 1.1, and its extended form is the first example of a proof by assertions in Section 1.2.1. Section 1.2.4 states four laws of congruences and Fermat's theorem. Section 1.2.5 gives the exponent of a prime in n!n!, and Section 1.2.8 finds the greatest common divisor of two Fibonacci numbers. The book proves most of these facts in a few lines and assumes the rest. This chapter proves all of them in full.

Section 5.1 starts from division with remainder. It defines the greatest common divisor, proves that Euclid's algorithm computes it, and extends the algorithm so that it also writes the divisor as a combination of the two inputs. Section 5.2 is about primes: the factorization of an integer into primes exists and is unique, there are infinitely many primes, and a short formula gives the exponent of a prime pp in n!n!. Section 5.3 is about congruences. It proves the laws of Section 1.2.4, solves linear congruences and proves the Chinese remainder theorem. Section 5.4 proves the theorems of Fermat, Euler and Wilson, and studies Euler's function φ\varphi.

The chapter uses the floor function and the binary operation  mod \bmod of Chapter 4. Recall from Section 4.3 that for integers aa and mm with m>0m > 0, the remainder a mod m=a−m⌊a/m⌋a \bmod m = a - m \lfloor a/m \rfloor is an integer in the range 0≤a mod m<m0 \le a \bmod m < m. The reader also needs proof by induction (P1) and the well ordering of the nonnegative integers: each nonempty set of nonnegative integers has a least element.

The chapter reads with Chapter 4 of Concrete Mathematics (GKP94, Sections 4.1 to 4.9). That book goes further, to the Stern-Brocot tree and the Möbius function. Those topics are not needed for Volume 1, and this chapter leaves them out. Chapter 10 uses the results of Section 5.1 again for the Fibonacci numbers, and P6 builds on all four sections.

All results here are about integers. The letters aa, bb, cc, dd, kk, mm, nn, qq, rr, xx and yy stand for integers, and pp stands for a prime, unless the text says otherwise.

The source of this page. Something is wrong? Report it.