taocp

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

Exercise · Volume 1 · 1.2.5-13

Exercise 1.2.5-13

proved in Lean solution written

RatingM23
MarkersM (mathematical)
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 1.2.5 of Volume 1. Companion chapter: Permutations and factorials

The task

Prove Wilson's theorem: for a prime pp, (p−1)! mod p=p−1(p-1)! \bmod p = p-1. Pair each number of {1,…,p−1}\{1,\ldots,p-1\} with its inverse modulo pp.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Show that each xx with 1≤x≤p−11 \le x \le p-1 has exactly one x′x' in the same range with xx′≡1(modp)xx' \equiv 1 \pmod p.

Hint 2

Find the xx with x′=xx' = x. Then the product of the other numbers splits into pairs with product 11.

Solution

Show the solution

For p=2p = 2, (p−1)!=1=p−1(p-1)! = 1 = p-1. Let p≥3p \ge 3.

Inverses. Let 1≤x≤p−11 \le x \le p-1. Since pp is prime, gcd⁡(x,p)=1\gcd(x, p) = 1, so there are integers u,vu, v with ux+vp=1ux + vp = 1. Then ux≡1(modp)ux \equiv 1 \pmod p, and the residue of uu modulo pp is not 00. Let x′x' be this residue, 1≤x′≤p−11 \le x' \le p-1. It is unique: if xy≡xz≡1xy \equiv xz \equiv 1 then x(y−z)≡0x(y - z) \equiv 0, so p∣(y−z)p \mid (y - z) since p∤xp \nmid x and pp is prime. Also (x′)′=x(x')' = x, so the map x↦x′x \mapsto x' is an involution of {1,…,p−1}\{1, \ldots, p-1\}.

Fixed points. x′=xx' = x means x2≡1x^2 \equiv 1, that is p∣(x−1)(x+1)p \mid (x-1)(x+1). Since pp is prime, p∣x−1p \mid x - 1 or p∣x+1p \mid x + 1, so x=1x = 1 or x=p−1x = p-1. These are two different numbers, because p≥3p \ge 3.

The remaining p−3p - 3 numbers split into (p−3)/2(p-3)/2 pairs {x,x′}\{x, x'\} with x≠x′x \ne x' and xx′≡1xx' \equiv 1. The product of the whole set is therefore

(p−1)!≡1⋅(p−1)⋅∏pairsxx′≡(p−1)(modp).(p-1)! \equiv 1 \cdot (p-1) \cdot \prod_{\text{pairs}} xx' \equiv (p-1) \pmod p.

Since 0<p−1<p0 < p - 1 < p, the remainder of (p−1)!(p-1)! modulo pp is p−1p-1.

Check

For p=7p = 7 the pairs are {2,4}\{2, 4\} and {3,5}\{3, 5\}, since 2⋅4=8≡12 \cdot 4 = 8 \equiv 1 and 3⋅5=15≡13 \cdot 5 = 15 \equiv 1. Then 6!=720=102⋅7+66! = 720 = 102 \cdot 7 + 6, and the remainder is 6=p−16 = p - 1. Python checks p=2,3,5,7,11,13p = 2, 3, 5, 7, 11, 13. For a composite number the rule fails: (6−1)!=120≡0(mod6)(6-1)! = 120 \equiv 0 \pmod 6, and (4−1)!=6≡2(mod4)(4-1)! = 6 \equiv 2 \pmod 4.

Remarks

The converse holds: if (n−1)!≡−1(modn)(n-1)! \equiv -1 \pmod n then nn is prime, so the congruence is a test for primes, but it is too slow to use. Exercise 1.2.5-14 gives a generalization to n! mod pn! \bmod p for every nn.

This solution is checked, and a person has not reviewed it yet. The source.

In Lean

proved The statement and its proof are in Lean. The kernel accepts the proof, and it uses the standard axioms only.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.