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
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 divides an integer when for some integer . The notation is . Then is a divisor of , and is a multiple of . When does not divide , the notation is .
Example P2.5.2. , since . Also , since . But : the multiples of near are and , and is neither of them. The divisors of are , , , , and .
Example P2.5.3. Each integer divides , since . In the other direction, only when , since for each . So is a multiple of every integer, and the only multiple of is itself.
Lemma P2.5.4 (rules of divisibility). For all integers , , , and :
- (i) ;
- (ii) if and , then ;
- (iii) if and , then ;
- (iv) if and , then ;
- (v) if and , then .
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) . (ii) If and , then . (iii) If and , then . (iv) If and , then , so and . (v) If , then , since means . If , then in the same way, and (iv) gives and .
Theorem P2.5.5 (division with remainder). Let and be integers with . There is exactly one pair of integers and with The pair is and .
Lean: Int.mul_ediv_add_emod, Int.emod_nonneg and Int.emod_lt_of_pos in the Lean core.
Proof. Existence. Put and . The floor satisfies . Multiply by : . Subtract : . Uniqueness. Let with both remainders in . Then , and . A multiple of with absolute value less than is . So , and then .
The numbers and are the quotient and the remainder. Section 4.3 studied for real and . Here only integers occur.
Definition P2.5.6 (greatest common divisor). Let and be integers that are not both . Their greatest common divisor is the largest integer that divides both and . Put .
Example P2.5.7. The positive divisors of are . Those of are . The common ones are . So .
Example P2.5.8. For , , because each integer divides and the largest divisor of is . Also , because and have the same divisors. The value always divides both numbers, so when and are not both .
Lemma P2.5.9 (the step of Euclid). Let . An integer divides both and if and only if divides both and . Hence
Lean: Nat.gcd_rec in the Lean core, in the form .
Proof. Write with . If and , then by rule (iii). If and , then by the same rule. So the two pairs have the same common divisors and the same largest common divisor.
Algorithm P2.5.10 (Euclid). The input is two integers and . The output is .
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 by the step of Euclid, and it makes smaller, since . A strictly decreasing sequence of nonnegative integers is finite, so the loop stops. At the end , and . The Proof part gives the details and a bound on the number of passes.
Theorem P2.5.11 (Bézout's identity). For all integers and , there are integers and with Each common divisor of and divides . When and are not both , the value is the least positive integer of the form .
Lean: Nat.gcd_eq_gcd_ab and Nat.dvd_gcd in Mathlib and the Lean core.
Proof. The extended algorithm below finds and . If and , then by rule (iii). The Proof part also gives a proof that does not use the algorithm.
Algorithm P2.5.12 (extended Euclid). The input is two integers and . The output is and integers and with . The algorithm keeps two rows , each with the property .
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 runs exactly as runs in Euclid's algorithm, so it stops with . The new row is the old row minus times the old row , so the property passes from the old rows to the new one.
Definition P2.5.13 (relatively prime). Two integers and are relatively prime, or coprime, when . The notation of GKP94 and of TAOCP is .
Example P2.5.14. , since the positive divisors of are , and only of them divides . But , since divides both.
Example P2.5.15. For each , . A common divisor divides , so . Also for each , and only for .
Corollary P2.5.16 (cancelling a coprime factor). If and , then .
Lean: Nat.Coprime.dvd_of_dvd_mul_left in the Lean core.
Proof. Bézout gives . Multiply by : . The number divides , and it divides because . So .
Definition P2.5.17 (least common multiple). Let and . Their least common multiple is the least positive integer that is a multiple of both and .
Example P2.5.18. The positive multiples of are , and those of are . The first common one is , so . Note that .
Example P2.5.19. If , then . If , then . For instance and .
Theorem P2.5.20 (gcd times lcm). For and , and each common multiple of and is a multiple of .
Lean: Nat.gcd_mul_lcm and Nat.lcm_dvd in the Lean core.
Proof. See the Proof part.
Proof
Euclid's algorithm is correct and stops. Let be the input, and let be the pair after passes of the loop. The invariant is It holds for . If it holds for and , then the next pair is , and the step of Euclid says that this pair has the same gcd. So the invariant holds for . Next, . The values are nonnegative integers, so there are at most of them, and the loop stops. It stops at a pass with . Then the output is .
A bound on the number of passes. The count is very weak. Let . A better bound comes from this fact: if , then . Indeed, if , then . If , then and . Now look at two passes. They take to , so the first entry of the pair falls from to less than in two passes, once . (After the first pass the pair always has .) So after passes the first entry is at most or the loop has stopped. The first entry is a positive integer after each pass. So for each such , and the number of passes is at most . Chapter 10 gives the exact worst case: consecutive Fibonacci numbers.
Bézout's identity without the algorithm. Let and be not both . Let be the set of positive integers of the form . The set is not empty: it holds . By well ordering has a least element . Divide by : with . Then If , then and , which is false. So and . In the same way . So is a common divisor, and . On the other side, divides , so by rule (iv). Hence . This proves all three claims of the theorem. The case is trivial: .
The extended algorithm is correct. Write the two rows after passes as and . The invariant is It holds at the start, since and . A pass replaces row by row , and row by row minus times row . 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 changes as changes in Euclid's algorithm, since when . So the loop stops, and at the end .
The gcd times the lcm. Let and write and . Then : a common divisor of and would make a common divisor of and larger than . Put . It is a common multiple of and . Let be any common multiple, . Then , so . Hence , and since , the corollary gives . Write . Then . So each common multiple is a multiple of . In particular each positive common multiple is at least , and . Finally .
Examples
Example P2.5.21. Euclid's algorithm on and . Each row shows the pair before a pass, the quotient and the new remainder.
| pass | ||||
|---|---|---|---|---|
| 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 , so . Indeed and , and . The run has many quotients equal to . 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 with . Each new row is the row two places up minus times the row one place up.
| row | ||||
|---|---|---|---|---|
| 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 : . The last row is a check: . Its entries are and , here and .
Example P2.5.23. The numbers and of Bézout are not unique. If , then also for each integer . For and the pairs are , for example and . The extended algorithm gives one pair with small entries.
Example P2.5.24. A use of the corollary. Find all integer solutions of . Divide by : . Then and , so . Write . Then . The solutions are for integers , and the least positive common multiple is .
Example P2.5.25. The step of Euclid in Section 1.2.8. Equation 1.2.8(9) says that divides and if and only if it divides and . Equation 1.2.8(8), the same claim with in place of , is the step that leads to it. With the invariant of Euclid's algorithm this gives eq. 1.2.8(7): . The argument is the proof of correctness above, with in place of . Chapter 10 gives it in full.
Exercises
- [10] Run Euclid's algorithm on and . Give each pair and the result.
- [M18] Find for each integer .
- [15] Run the extended algorithm on and . Give integers and with .
- [M20] Prove that for each integer .
- [M22] Prove that for positive , and . Show by an example that need not equal .
- [12] A machine takes coins of and units only. Which amounts from to can it take, with any number of coins of each kind? Which amounts can it take in general?
- [M24] Let . Prove that the extended algorithm returns and with and , where .
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 for the integers and 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 of Chapter 4 when the divisor is positive. TAOCP's for real arguments and for is TAOCP.kmod in TAOCP/Notation (tamnd/taocp-lean main). The theorem TAOCP.floor_mul_add_kmod is the equation , and TAOCP.kmod_intCast_natCast says that TAOCP.kmod agrees with Int.emod for an integer and a natural divisor .
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 and 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 and 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 is a product of primes in exactly one way, that there are infinitely many primes, and that the exponent of a prime in is a sum of floors. The last result is eq. 1.2.5(8). The letter always stands for a prime.
Definition P2.5.26 (prime). An integer is prime when its only positive divisors are and . An integer that is not prime is composite. The integer is neither prime nor composite.
Example P2.5.27. The primes below are . There are of them. The number is the only even prime, since each other even number has the divisor .
Example P2.5.28. The number looks prime, but . To test , it is enough to try the divisors with : if with , then . For the trial divisors are , none of them divides , so is prime.
Lemma P2.5.29 (a least divisor is prime). Each integer has a prime divisor. The least divisor of is prime.
Lean: Nat.exists_prime_and_dvd and Nat.minFac_prime in Mathlib.
Proof. The set of divisors of holds , so it has a least element . If had a divisor with , then by transitivity, and would be a smaller divisor of . So is prime.
Lemma P2.5.30 (Euclid's lemma). If , then or . More generally, if divides a product , then it divides one of the factors.
Lean: Nat.Prime.dvd_mul in Mathlib.
Proof. Let and . The gcd of and is a positive divisor of , so it is or . It is not , since . So , and the corollary of Section 5.1 on cancelling a coprime factor gives . The general form follows by induction on , with and .
Theorem P2.5.31 (unique factorization). Each integer is a product of primes, and the list is the same for each such product. The case is the empty product, with .
Lean: Nat.prod_primeFactorsList and Nat.primeFactorsList_unique in Mathlib.
Proof. See the Proof part.
Group the equal primes: , where the product runs over all primes and only finitely many exponents are not . This is the canonical form of .
Definition P2.5.32 (exponent of a prime). For , the exponent is the exponent of in the canonical form of . Equally, is the largest with .
Example P2.5.33. , so , , and for each other prime .
Example P2.5.34. , since the canonical form of is the product of those of and . For instance , and .
Corollary P2.5.35 (gcd and lcm by exponents). For and and each prime ,
Lean: Nat.factorization_gcd and Nat.factorization_lcm in Mathlib.
Proof. By uniqueness, if and only if for each . So the common divisors of and are the with for each , and the largest of them has equality at each . The lcm is the same with and .
Theorem P2.5.36 (infinitely many primes). For each there is a prime .
Lean: Nat.exists_infinite_primes in Mathlib, in the form .
Proof. Let . By the first lemma, has a prime divisor . If , then , and so , which is false. So .
Theorem P2.5.37 (Legendre's formula). For each prime and each , The sum is finite: its terms are once . This is eq. 1.2.5(8).
Lean: padicValNat_factorial in Mathlib, with the sum over for any with .
Proof. See the Proof part.
Corollary P2.5.38 (Legendre's formula by digits). Let be the sum of the digits of in base . Then
Lean: Nat.Prime.sub_one_mul_multiplicity_factorial in Mathlib, in the form .
Proof. See the Proof part.
Proof
Existence of the factorization. Induction on . For the empty product works. Let and suppose each with is a product of primes. By the first lemma has a prime divisor . Then with . The induction hypothesis writes as a product of primes, and the factor completes a product for . Sort the factors to get .
Uniqueness of the factorization. Suppose that some has two different sorted lists, and let be the least such number. Write Here , since has only the empty list. So and . The prime divides , so by Euclid's lemma for some . The only prime divisor of the prime is , so . Cancel this prime from both products. The quotient has the two lists with one copy of removed. These two lists are different, since the full lists were different and the same prime left both. But , and was the least number with two lists. This is a contradiction. So no 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 among the numbers .
Legendre's formula. Write . Since of a product is the sum of the values of the factors, Now count in a different order. The value is the number of with . So The inner sum counts the multiples of in . These are , so there are of them. This gives the formula. The exchange of the two sums is allowed, since only finitely many terms are not (Chapter 1 gives the rules for exchanging sums).
Legendre's formula by digits. Write in base , with digits . Then for , since the rest is less than . Add over . The digit appears in the terms , with weights . Their sum is So
Examples
Example P2.5.39. The exponents of the small primes in . For the terms are For : . For : . For : . The digit form checks the first value: has three digits, and . It also checks the value for : has digit sum , and .
Example P2.5.40. The number of zeros at the end of in decimal is , since . The exponent of is the smaller one, since each term is at most the term . So ends in zeros, and ends in zeros.
Example P2.5.41. The proof of the infinity of primes does not claim that is prime. For , : the new prime is . For , . A similar product, , is , and both factors exceed , as the argument says they must.
Example P2.5.42. The gcd and the lcm by exponents. Take and . Then The check of Section 5.1 holds: .
Example P2.5.43. The canonical form gives the number of divisors. A positive divisor of is with , and each choice of the gives a different divisor by uniqueness. So has positive divisors. For that is .
Exercises
- [10] Factor and into primes.
- [15] Compute and its prime factors. Explain without the factors why no prime divides .
- [M20] Prove that is composite for each integer .
- [12] Compute , , and . How many zeros are at the end of in decimal?
- [M20] How many zeros are at the end of when it is written in base ?
- [M20] Prove that , where is the number of digits of in binary. Find all with .
- [M25] Let , and let be the largest power of the prime that divides . Prove that .
- [M22] Prove that an integer 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 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 -adic valuations. It states for each bound , 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 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 is a positive integer.
Definition P2.5.44 (congruence). Let . Two integers and are congruent modulo when . The notation is . The number is the modulus.
Example P2.5.45. , since . Also , since . But , since is not a multiple of .
Example P2.5.46. Modulo , the integers fall into two classes: for even and for odd . Modulo , all integers are congruent, since divides each difference. A clock shows the hour modulo : hours after o'clock it is o'clock, since .
Lemma P2.5.47 (two forms of the definition). if and only if . In particular , and each integer is congruent to exactly one of .
Lean: Nat.ModEq, defined by , and Nat.modEq_iff_dvd in Mathlib.
Proof. Write and with remainders and in . Then . So if and only if . Since , this holds if and only if .
The second form is the definition of eq. 1.2.4(5). TAOCP states it for real , and , with the 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 is reflexive, symmetric and transitive: ; if , then ; if and , then , all modulo .
Lean: Nat.ModEq.refl, Nat.ModEq.symm and Nat.ModEq.trans in Mathlib.
Proof. ; if , then ; if and , then .
The classes of the relation are the residue classes modulo . By the lemma there are exactly of them, one for each remainder.
Theorem P2.5.49 (Law A: addition and multiplication). If and , then Hence for each .
Lean: Nat.ModEq.add, Nat.ModEq.mul and Nat.ModEq.pow in Mathlib; Int.ModEq.sub for the difference.
Proof. Let and . Then . For the product, The rule for powers follows by induction on from the rule for products.
Theorem P2.5.50 (Law B: cancellation). If and , and if , then .
Lean: Nat.ModEq.cancel_left_div_gcd in Mathlib, in the general form: from it gives .
Proof. By Law A, , so , that is . Since , the corollary of Section 5.1 on cancelling a coprime factor gives .
Theorem P2.5.51 (Law C: scaling the modulus). Let . Then if and only if .
Lean: Nat.ModEq.mul_left_cancel_iff' in Mathlib.
Proof. if and only if , since can be cancelled in . And .
TAOCP states Law C with the modulus for each , since its congruence allows a negative modulus. This chapter keeps the modulus positive, so it writes .
Theorem P2.5.52 (Law D: splitting the modulus). Let . Then if and only if and .
Lean: Nat.modEq_and_modEq_iff_modEq_mul in Mathlib.
Proof. If , then and . For the other direction, let with . Since , the corollary of Section 5.1 gives , so .
Definition P2.5.53 (inverse modulo m). An inverse of modulo is an integer with .
Example P2.5.54. Modulo , the inverse of is , since . The inverses of modulo are . Each nonzero class modulo has an inverse.
Example P2.5.55. Modulo , the number has no inverse. The products are , and modulo they are always , or , never . The classes with inverses modulo are , and each is its own inverse: , , are all .
Theorem P2.5.56 (existence of inverses). The integer has an inverse modulo if and only if . The inverse is unique modulo , and the extended algorithm of Section 5.1 computes it.
Lean: ZMod.coe_mul_inv_eq_one and ZMod.unitOfCoprime in Mathlib.
Proof. If , Bézout gives , so . If , then for some , and each common divisor of and divides . If , then Law B, with , gives .
Theorem P2.5.57 (linear congruences). Let . The congruence has a solution if and only if . In that case it has exactly solutions modulo . They are where 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.
Theorem P2.5.58 (Chinese remainder theorem). Let be positive and pairwise coprime, and let . For all integers , the system has a solution, and the solution is unique modulo .
Lean: Nat.chineseRemainder for two moduli and Nat.chineseRemainderOfList for a list in Mathlib; ZMod.chineseRemainder states the ring isomorphism .
Proof. See the Proof part.
Proof
Linear congruences. First let . Write , and . Then , as in the proof of the gcd times the lcm in Section 5.1. By Law C with , Since , the number has an inverse modulo . Multiply by it (Law A), and in the other direction multiply by : the congruence holds if and only if . So the solutions form one class modulo . Modulo this class splits into the classes for . They are different modulo , since the differences with are not multiples of .
Now let . If , then for some , and divides the right side. So no solution exists.
The Chinese remainder theorem. Uniqueness. If and both solve the system, then for each . Law D, applied times, joins these congruences to . Each step is allowed: is coprime to , since a prime divisor of the product divides one of the factors by Euclid's lemma, and .
Existence. Put . Then , by the same argument. Let be an inverse of modulo . Put Modulo , each term with is , since . The term with is . So for each .
There is also a short proof by counting. The map from the classes modulo to the lists of classes modulo is one to one, by uniqueness. Both sets have 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 .
Examples
Example P2.5.59. The inverse of modulo . The extended algorithm in Section 5.1 gave . Divide by : . So , and the inverse is . Check: .
Example P2.5.60. Solve . Here and , so there are solutions modulo . Divide by : . The inverse of modulo is , since . So . The solutions modulo are Check one: .
Example P2.5.61. Solve , , . The moduli are pairwise coprime, and . Then , , . The inverses: has inverse ; has inverse ; has inverse . So Check: .
Example P2.5.62. Casting out nines. Since , Law A gives , and a number is congruent modulo to the sum of its decimal digits. So . In the same way , and a number is congruent modulo to the alternating sum of its digits, starting with the units digit: , and indeed .
Example P2.5.63. Cancellation without the coprime condition. From one may not cancel to get : for and , both sides are and . Law C with turns into , and Law B with then gives . That is the general rule of the Lean name above: the modulus becomes .
Exercises
- [10] Decide, with a reason, whether and whether .
- [M15] Prove that for each prime .
- [15] Find the inverse of modulo with the extended algorithm.
- [20] Find all solutions of .
- [20] Find the least positive with , and .
- [M18] Since , show that a number is congruent modulo to the alternating sum of its blocks of three decimal digits, from the right. Use this to find modulo , and .
- [M22] Prove that has only the solutions for a prime . Then find all solutions of .
- [M24] Prove that and hold together if and only if . Show that Law D is a special case.
Lean
Mathlib writes 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 in ZMod n when . The Chinese remainder theorem is Nat.chineseRemainder (a solution with its two congruences), Nat.chineseRemainder_lt_mul (the solution below ) 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 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 , 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 . 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 in place of . Wilson's theorem gives . All three rest on one idea: multiplication by a number coprime to permutes the residue classes that are coprime to .
Theorem P2.5.64 (Fermat's theorem). Let be prime. For each integer , If , then also .
Lean: ZMod.pow_card (the form 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.
Definition P2.5.65 (Euler's function). For , is the number of integers with and .
Example P2.5.66. The integers in that are coprime to are . So . For a prime , each of is coprime to , and is not, so .
Example P2.5.67. The first values are Here , because . The values are even from on, since exactly when , and the two numbers and are different unless , a case with for .
Lemma P2.5.68 (prime powers). For a prime and , .
Lean: Nat.totient_prime_pow and Nat.totient_prime in Mathlib.
Proof. A number fails to be coprime to exactly when . In there are multiples of .
Theorem P2.5.69 (multiplicativity of ). If , then . Hence
Lean: Nat.totient_mul and Nat.totient_eq_prod_factorization in Mathlib.
Proof. See the Proof part.
Theorem P2.5.70 (the divisor sum of ). For each , where the sum runs over the positive divisors of .
Lean: Nat.sum_totient in Mathlib.
Proof. See the Proof part.
Theorem P2.5.71 (Euler's theorem). If , then .
Lean: Nat.ModEq.pow_totient and ZMod.pow_totient in Mathlib.
Proof. See the Proof part.
Theorem P2.5.72 (Wilson's theorem). An integer is prime if and only if .
Lean: ZMod.wilsons_lemma (one direction) and Nat.prime_iff_fac_equiv_neg_one (both) in Mathlib.
Proof. See the Proof part.
Proof
Fermat, first proof. This is the proof of Section 1.2.4. Let . Consider the numbers None of them is : with would give by Euclid's lemma, but . No two are equal: gives by Law B, since , and two numbers in that are congruent modulo are equal. So the list holds different values in . It is a permutation of . Multiply the list (Law A): The number is coprime to , since no factor of it has the factor . Law B cancels it: . Multiply by to get . If , then both sides of are .
Fermat, second proof. This proof is not the one in TAOCP. Let , and think of as the size of an alphabet. There are strings of length . Of these, strings are constant, such as . Call two strings equivalent when one is a cyclic shift of the other. Let be a string that is not constant, and let be the least positive shift that maps to itself. The shift by maps to itself. The shifts that fix are closed under sums and differences, so by Bézout the shift by also fixes . Hence . Since is prime, or . If , then is constant. So , and the shifts of are all different. So the strings that are not constant fall into classes of exactly strings each, and This proves the theorem for . For it is clear. For , use with , and Law A.
Multiplicativity of . Let . By the Chinese remainder theorem of Section 5.3, the map is a one to one correspondence between the classes modulo and the pairs of classes. A number is coprime to if and only if it is coprime to and to , since a prime divides only if it divides or . Also depends only on , since . So the correspondence maps the classes coprime to onto the pairs of a class coprime to and a class coprime to . There are such pairs. The product formula follows from the lemma on prime powers: the prime powers of are pairwise coprime.
The divisor sum. Sort the numbers by the value of . For a divisor of , the numbers with are with and , by the rule (exercise 5.1-4). There are of them. So . As runs over the divisors of , so does .
Euler's theorem. Let be the numbers in that are coprime to . The numbers are coprime to , since and are. They are different, by Law B. So they are the in another order. Multiply them all: The product of the is coprime to , and Law B cancels it. For a prime this is the first proof of Fermat again, since .
Wilson's theorem. Let be prime. For , . Let . Each in has an inverse in the same set (Section 5.3). The inverse is itself exactly when , that is . By Euclid's lemma this means or . So the numbers fall into pairs with , and their product is . Hence .
For the other direction, let be composite, with a divisor and . Then , since . If , then , and so , which is false. So the congruence fails for each composite .
Examples
Example P2.5.73. The permutation of the first proof for and . The numbers for are each of once. So , and indeed for each from to .
Example P2.5.74. Powers with Fermat. To find , use and : . In general for .
Example P2.5.75. Euler's function by the product formula. , so Then Euler's theorem gives the last three digits of a power. Since and , . The value of is , so ends in the digits .
Example P2.5.76. The divisor sum for . Write the fractions for in lowest terms: The denominators are divisors of , and the denominator appears with the numerators coprime to . The counts for are , and their sum is . This is the proof of the theorem, seen on one case.
Example P2.5.77. Wilson's pairs for . The inverses modulo pair the numbers as with products , all . So . For : , and .
Example P2.5.78. Fermat's theorem does not decide primality. The composite number satisfies . But , so the base shows that is not prime. Some composite numbers pass the test for each base coprime to them; exercise 5.4-4 gives one.
Exercises
- [10] Compute , and .
- [12] Find and .
- [M18] Find the last two decimal digits of .
- [M20] Let . Prove that for each , although is not prime.
- [M20] Find all with .
- [M22] Prove that for each prime . Then find .
- [M23] Prove that for each integer .
- [M24] Prove that for the sum of the numbers with and is .
Lean
Mathlib defines Nat.totient n as the number of with , with the notation φ n. For this agrees with the definition of the text, since only for , and then the text counts 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 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 . 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 . The name 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 matters: a string of prime length has no period between and . 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 reduce the exponent modulo .
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 , 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 in . 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 .
The chapter uses the floor function and the binary operation of Chapter 4. Recall from Section 4.3 that for integers and with , the remainder is an integer in the range . 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 , , , , , , , , , and stand for integers, and stands for a prime, unless the text says otherwise.
The source of this page. Something is wrong? Report it.