Skip to content

Congruences and Fermat's Little Theorem: Computing in a World of Remainders

Prerequisite:Primes and the Prime Number Theorem: From Infinitude and Unique Factorization to the Law of Distribution

Raw
  • Sorting the integers by “the remainder on division by nn” carries addition, subtraction, and multiplication along unchanged. This is the congruence a≡b(modn)a \equiv b \pmod n, and the quotient set Z/nZ\mathbb{Z}/n\mathbb{Z} becomes a commutative ring.
  • Division, by contrast, is not always available. The class [a][a] has an inverse in Z/nZ\mathbb{Z}/n\mathbb{Z} exactly when gcd⁡(a,n)=1\gcd(a,n)=1, and the sole reason for this is Bézout’s identity.
  • When nn is a prime pp, the ring Z/pZ\mathbb{Z}/p\mathbb{Z} is a field: every nonzero element is invertible. Fermat’s little theorem ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p (for p∤ap \nmid a) follows from this.
  • The extension to general nn is Euler’s theorem aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod n, whose proof has exactly the same skeleton as Fermat’s: multiplication by aa is a permutation of the group of units.
  • The Chinese remainder theorem is the isomorphism Z/mnZ≅Z/mZ×Z/nZ\mathbb{Z}/mn\mathbb{Z} \cong \mathbb{Z}/m\mathbb{Z} \times \mathbb{Z}/n\mathbb{Z} (for gcd⁡(m,n)=1\gcd(m,n)=1), and it supports both the multiplicativity of φ\varphi and the correctness of RSA.
  • The converse of Fermat’s little theorem is false. Carmichael numbers such as 561561 are counterexamples, and it is they that pushed primality testing towards probabilistic algorithms.

1. Motivation — arithmetic on a clock face

Section titled “1. Motivation — arithmetic on a clock face”

Suppose it is 77 o’clock; what time will it be 88 hours from now? We compute 7+8=157+8=15, but there is no 1515 on a clock face, so we answer 33 o’clock. In everyday life we are already adding inside the world of “remainders on division by 1212“.

012345678910117 + 8= 3 (mod 12)
Addition modulo 12. Advancing 8 from 7 brings us back to 3.

This way of computing with remainders alone had been used piecemeal for a long time. Casting out nines, which detects multiples of 99 from the sum of the digits, was a medieval merchant’s device for checking arithmetic, and calendar computations are in essence computations with remainders. But it was Gauss, in the Disquisitiones Arithmeticae (1801), who systematized the practice with the symbol ≡\equiv and treated it as an algebraic system in its own right. With this single piece of notation Gauss unified techniques of number theory that had until then been scattered.

Why should merely introducing a symbol produce progress? Because congruences obey almost the same rules as equalities. “One may add the same thing to both sides” and “one may multiply both sides by the same thing” both hold, so our instincts for manipulating equations carry over intact. As a result a problem such as finding the last two digits of 21002^{100} — a computation with a 3131-digit number if carried out honestly — turns into a handful of multiplications of numbers below 100100.

There is, however, one decisive difference from equality: division is not free. We have 2×3≡2×9(mod12)2 \times 3 \equiv 2 \times 9 \pmod{12} while 3≢9(mod12)3 \not\equiv 9 \pmod{12}. Pinning down exactly where the boundary between “divisible” and “not divisible” lies is the technical heart of this article, and from it flow both Fermat’s little theorem and RSA.

This article is the sequel to The charm of primes — the prime number theorem. Think of it as looking at primes from the side of “how they behave” rather than “how many there are”.

2. Preliminaries — the division algorithm and Bézout’s identity

Section titled “2. Preliminaries — the division algorithm and Bézout’s identity”

Throughout, Z\mathbb{Z} denotes the integers and N={1,2,3,…}\mathbb{N} = \{1,2,3,\ldots\} (excluding 00). We write a∣ba \mid b for ”aa divides bb”, that is, for the existence of some c∈Zc \in \mathbb{Z} with b=acb = ac.

Theorem 2.1(Division algorithm)

Let a∈Za \in \mathbb{Z} and n∈Nn \in \mathbb{N}. Then there is exactly one pair of integers q,rq, r with

a=qn+r,0≤r<n.a = qn + r, \qquad 0 \le r < n .
Proof(Theorem 2.1)

We first prove existence. Consider the set S={a−qn∣q∈Z}∩Z≥0S = \{a - qn \mid q \in \mathbb{Z}\} \cap \mathbb{Z}_{\ge 0}. Taking qq to be a sufficiently small negative integer (for instance q=−∣a∣q = -|a|) gives a−qn=a+∣a∣n≥a+∣a∣≥0a - qn = a + |a|n \ge a + |a| \ge 0, so S≠∅S \ne \varnothing. Being a nonempty set of nonnegative integers, SS has a least element r=a−qnr = a - qn by well-ordering(Axiom 3.1)[Techniques of Proof].

If r≥nr \ge n, then r−n=a−(q+1)nr - n = a - (q+1)n is again nonnegative and lies in SS, while r−n<rr - n < r, contradicting the minimality of rr. Hence 0≤r<n0 \le r < n.

Now uniqueness. Suppose a=qn+r=q′n+r′a = qn + r = q'n + r' with 0≤r,r′<n0 \le r, r' < n. Then (q−q′)n=r′−r(q - q')n = r' - r. The right-hand side satisfies ∣r′−r∣<n|r' - r| < n, so ∣q−q′∣⋅n<n|q - q'| \cdot n < n, hence ∣q−q′∣<1|q - q'| < 1, that is, q=q′q = q'. Substituting back gives r=r′r = r'.

We write this rr as a mod na \bmod n. This is the meaning of  mod \bmod when it is used as “the operation of taking the remainder” (the (modn)\pmod n that appears later is a parenthetical indicating a relation, and plays a different role).

When aa and bb are not both 00, we write gcd⁡(a,b)\gcd(a,b) for the largest common divisor of aa and bb. When gcd⁡(a,b)=1\gcd(a,b)=1 we say that aa and bb are coprime (the notation agrees with Definition 2.1[Primes and the Prime Number Theorem]).

Theorem 2.2(Bézout's identity)

Let a,ba, b be integers, not both 00. Then there exist integers x,yx, y with

ax+by=gcd⁡(a,b).ax + by = \gcd(a,b) .

Moreover, {ax+by∣x,y∈Z}\{ax+by \mid x,y \in \mathbb{Z}\} coincides with the set of all multiples of gcd⁡(a,b)\gcd(a,b).

Proof(Theorem 2.2)

Put I={ax+by∣x,y∈Z}I = \{ax + by \mid x, y \in \mathbb{Z}\} and let I+I^{+} be the set of positive integers in II. If a≠0a \ne 0 then a⋅a+b⋅0=a2>0a \cdot a + b \cdot 0 = a^2 > 0 lies in I+I^{+}; if a=0a = 0 then b≠0b \ne 0, so b2∈I+b^2 \in I^{+}. Either way I+≠∅I^{+} \ne \varnothing, so by well-ordering it has a least element d=ax0+by0>0d = ax_0 + by_0 > 0.

We first show d∣ad \mid a. By Theorem 2.1 write a=qd+ra = qd + r with 0≤r<d0 \le r < d; then

r=a−qd=a−q(ax0+by0)=a(1−qx0)+b(−qy0)∈I.r = a - qd = a - q(ax_0 + by_0) = a(1 - qx_0) + b(-qy_0) \in I .

If r>0r > 0 then r∈I+r \in I^{+} and r<dr < d, contradicting the minimality of dd. Hence r=0r = 0, that is, d∣ad \mid a. The same argument gives d∣bd \mid b, so dd is a common divisor of aa and bb.

Conversely, let cc be any common divisor of aa and bb. Both terms on the right of d=ax0+by0d = ax_0 + by_0 are divisible by cc, so c∣dc \mid d and therefore c≤dc \le d. Hence d=gcd⁡(a,b)d = \gcd(a,b), and d=ax0+by0d = ax_0 + by_0 is the representation sought.

The last assertion follows because every element ax+byax+by of II is a multiple of dd (since d∣ad \mid a and d∣bd \mid b), while conversely every multiple kd=a(kx0)+b(ky0)kd = a(kx_0) + b(ky_0) of dd lies in II.

Lemma 2.3(Euclid's lemma)

If gcd⁡(c,n)=1\gcd(c,n) = 1 and n∣cmn \mid cm, then n∣mn \mid m. In particular, if pp is prime and p∣abp \mid ab, then p∣ap \mid a or p∣bp \mid b.

Proof(Lemma 2.3)

By Theorem 2.2 there are integers x,yx, y with cx+ny=1cx + ny = 1. Multiplying both sides by mm gives

m=(cm)x+n(my).m = (cm)x + n(my) .

By hypothesis n∣cmn \mid cm, so the first term on the right is divisible by nn, and the second term is plainly a multiple of nn. Hence n∣mn \mid m.

For the second statement, suppose p∤ap \nmid a. Since pp is prime, gcd⁡(p,a)\gcd(p,a) is either 11 or pp, and pp is excluded, so gcd⁡(p,a)=1\gcd(p,a)=1. Applying the first part with c=ac = a, n=pn = p, m=bm = b yields p∣bp \mid b.

Definition 3.1(Congruence)

Let n∈Nn \in \mathbb{N}. For integers a,ba, b, if n∣(a−b)n \mid (a - b) we say that aa and bb are congruent modulo nn and write

a≡b(modn).a \equiv b \pmod n .

We call nn the modulus of the congruence.

Proposition 3.2

Congruence modulo nn is an equivalence relation on Z\mathbb{Z}, and it has exactly nn equivalence classes, namely the sets of integers congruent to each of 0,1,…,n−10, 1, \ldots, n-1.

Proof(Proposition 3.2)

Reflexivity holds because n∣0=a−an \mid 0 = a - a. Symmetry follows because if n∣(a−b)n \mid (a-b) then b−a=−(a−b)b - a = -(a-b) is also a multiple of nn. For transitivity, if a−b=nka - b = nk and b−c=nlb - c = nl then a−c=n(k+l)a - c = n(k+l).

Now we count the classes. By Theorem 2.1 every aa can be written a=qn+ra = qn + r with 0≤r<n0 \le r < n, so a−r=qna - r = qn, that is, a≡r(modn)a \equiv r \pmod n. Hence each class contains one of 0,…,n−10,\ldots,n-1. Moreover if 0≤r<r′<n0 \le r < r' < n then 0<r′−r<n0 < r' - r < n, so n∤(r′−r)n \nmid (r' - r) and rr is not congruent to r′r'. Therefore there are exactly nn classes.

For equivalence relations in general, see Relations and equivalence relations — what does “the same” mean? (the first half of the proposition above is Proposition 3.4[関係と同値関係]). We write [a][a] or [a]n[a]_n for the class containing aa and call it the residue class of aa.

Proposition 3.3(Arithmetic of congruences)

Suppose a≡b(modn)a \equiv b \pmod n and c≡d(modn)c \equiv d \pmod n. Then

a+c≡b+d,a−c≡b−d,ac≡bd(modn).a + c \equiv b + d, \qquad a - c \equiv b - d, \qquad ac \equiv bd \pmod n .

In particular ak≡bk(modn)a^k \equiv b^k \pmod n for every k∈Nk \in \mathbb{N}.

Proof(Proposition 3.3)

By hypothesis there are integers s,ts,t with a−b=nsa - b = ns and c−d=ntc - d = nt. For addition,

(a+c)−(b+d)=(a−b)+(c−d)=n(s+t)(a+c) - (b+d) = (a-b) + (c-d) = n(s+t)

is divisible by nn. Subtraction is identical up to a change of sign. For multiplication,

ac−bd=ac−bc+bc−bd=c(a−b)+b(c−d)=n(cs+bt),ac - bd = ac - bc + bc - bd = c(a-b) + b(c-d) = n(cs + bt) ,

so n∣(ac−bd)n \mid (ac - bd). Here we used the device of adding and subtracting bcbc.

The statement about powers is proved by induction on kk. The case k=1k=1 is the hypothesis itself. If ak≡bka^k \equiv b^k, applying the multiplicative statement to this and to a≡ba \equiv b gives ak+1≡bk+1a^{k+1} \equiv b^{k+1}.

It is Proposition 3.3 that licenses us to treat congruences like equations. Thanks to it, enormous powers can be tracked using remainders alone.

Example 3.4(The last two digits of 2 to the 100th power)

We compute 2100 mod 1002^{100} \bmod 100. We have 210=1024≡24(mod100)2^{10} = 1024 \equiv 24 \pmod{100}. Squaring both sides using Proposition 3.3,

220≡242=576≡76(mod100).2^{20} \equiv 24^2 = 576 \equiv 76 \pmod{100}.

Furthermore 762=5776≡76(mod100)76^2 = 5776 \equiv 76 \pmod{100}, so 240≡762^{40} \equiv 76 and 280≡762^{80} \equiv 76. Therefore

2100=280⋅220≡76⋅76=5776≡76(mod100).2^{100} = 2^{80} \cdot 2^{20} \equiv 76 \cdot 76 = 5776 \equiv 76 \pmod{100}.

The number 21002^{100} has 3131 digits, yet we found its last two digits to be 7676 without ever multiplying numbers of more than three digits.

Example 3.5(Casting out nines and the test for 11)

Since 10≡1(mod9)10 \equiv 1 \pmod 9, Proposition 3.3 gives 10k≡1(mod9)10^k \equiv 1 \pmod 9. Hence for N=∑kdk10kN = \sum_{k} d_k 10^k (where the dkd_k are the digits),

N≡∑kdk(mod9).N \equiv \sum_k d_k \pmod 9 .

This proves that multiples of 99 can be detected from the digit sum. For example, if N=987654N = 987654 then 9+8+7+6+5+4=39≡3+9=12≡3(mod9)9+8+7+6+5+4 = 39 \equiv 3+9 = 12 \equiv 3 \pmod 9, so N≡3(mod9)N \equiv 3 \pmod 9.

On the other hand 10≡−1(mod11)10 \equiv -1 \pmod{11}, so 10k≡(−1)k10^k \equiv (-1)^k and

N≡∑k(−1)kdk(mod11).N \equiv \sum_k (-1)^k d_k \pmod{11} .

For 987654987654, attaching alternating signs starting from the units digit gives 4−5+6−7+8−9=−3≡8(mod11)4 - 5 + 6 - 7 + 8 - 9 = -3 \equiv 8 \pmod{11}.

Division alone is a different matter. The next proposition is the precise rule for “dividing both sides” of a congruence.

Proposition 3.6(Cancellation law)

Let n∈Nn \in \mathbb{N}, c∈Zc \in \mathbb{Z}, and d=gcd⁡(c,n)d = \gcd(c,n). Then

ca≡cb(modn)  ⟺  a≡b(modn/d).ca \equiv cb \pmod n \iff a \equiv b \pmod{n/d} .

In particular, one may conclude a≡b(modn)a \equiv b \pmod n from ca≡cb(modn)ca \equiv cb \pmod n precisely when gcd⁡(c,n)=1\gcd(c,n)=1.

Proof(Proposition 3.6)

Write c=dc′c = dc' and n=dn′n = dn'; then gcd⁡(c′,n′)=1\gcd(c', n') = 1 (if e>1e > 1 were a common divisor of the two, then dede would be a common divisor of cc and nn, contradicting the maximality of dd).

(⇒)(\Rightarrow) The congruence ca≡cb(modn)ca \equiv cb \pmod n says n∣c(a−b)n \mid c(a-b), that is, dn′∣dc′(a−b)dn' \mid dc'(a-b); dividing both sides by dd gives n′∣c′(a−b)n' \mid c'(a-b). Since gcd⁡(c′,n′)=1\gcd(c',n')=1, Lemma 2.3 yields n′∣(a−b)n' \mid (a-b), that is, a≡b(modn′)a \equiv b \pmod{n'}.

(⇐)(\Leftarrow) If n′∣(a−b)n' \mid (a-b) then n=dn′∣dc′(a−b)=c(a−b)n = dn' \mid dc'(a-b) = c(a-b), so ca≡cb(modn)ca \equiv cb \pmod n.

Example 3.7(An example where division breaks down)

We have 2⋅3=62 \cdot 3 = 6 and 2⋅9=182 \cdot 9 = 18, and 18−6=1218 - 6 = 12, so 2⋅3≡2⋅9(mod12)2 \cdot 3 \equiv 2 \cdot 9 \pmod{12}. But 9−3=69 - 3 = 6 is not a multiple of 1212, so 3≢9(mod12)3 \not\equiv 9 \pmod{12}. As Proposition 3.6 says, here d=gcd⁡(2,12)=2d = \gcd(2,12) = 2, so the most one may correctly conclude is 3≡9(mod6)3 \equiv 9 \pmod 6 — and indeed 9−3=69 - 3 = 6 satisfies this.

Definition 4.1(Ring of residue classes)

Let n∈Nn \in \mathbb{N}. We write

Z/nZ={[0],[1],…,[n−1]}\mathbb{Z}/n\mathbb{Z} = \{[0], [1], \ldots, [n-1]\}

for the set of all residue classes modulo nn, with operations defined by

[a]+[b]:=[a+b],[a]⋅[b]:=[ab].[a] + [b] := [a+b], \qquad [a] \cdot [b] := [ab] .

There is something to be verified in this definition. The symbol [a][a] denotes an equivalence class, and there is freedom in the choice of the representative aa. Unless [a+b]=[a′+b′][a+b] = [a'+b'] and [ab]=[a′b′][ab] = [a'b'] whenever [a]=[a′][a] = [a'] and [b]=[b′][b] = [b'], the operations are not well defined. But this is precisely the content of Proposition 3.3. Hence the operations are independent of the choice of representatives (the same statement phrased in the language of quotient sets is Theorem 5.4[関係と同値関係]).

Proposition 4.2

With the operations of Definition 4.1, the set Z/nZ\mathbb{Z}/n\mathbb{Z} is a commutative ring with identity [1][1].

Proof(Proposition 4.2)

Associativity, commutativity, and distributivity all follow at once from the corresponding laws in Z\mathbb{Z}. For instance, distributivity reads

[a]([b]+[c])=[a][b+c]=[a(b+c)]=[ab+ac]=[ab]+[ac]=[a][b]+[a][c],[a]([b]+[c]) = [a][b+c] = [a(b+c)] = [ab+ac] = [ab]+[ac] = [a][b]+[a][c] ,

where only the third equality uses distributivity in Z\mathbb{Z}; the others merely rewrite the definition. The additive identity is [0][0], the additive inverse of [a][a] is [−a][-a], and the multiplicative identity is [1][1].

For ring theory in general see Foundations of rings and fields, and for the viewpoint of Z/nZ\mathbb{Z}/n\mathbb{Z} as the quotient ring by the ideal nZn\mathbb{Z} see Theorem 4.2[イデアルと剰余環] in Ideals and quotient rings.

Definition 4.3(Group of units)

An element [a][a] of the ring Z/nZ\mathbb{Z}/n\mathbb{Z} is called a unit if there is some [b][b] with [a][b]=[1][a][b] = [1]. The set of all units is written (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^{\times}.

Proposition 4.4

An element [a]∈Z/nZ[a] \in \mathbb{Z}/n\mathbb{Z} is a unit if and only if gcd⁡(a,n)=1\gcd(a,n) = 1. Moreover (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^{\times} is a group under multiplication.

Proof(Proposition 4.4)

(⇐)(\Leftarrow) If gcd⁡(a,n)=1\gcd(a,n)=1, then by Theorem 2.2 there are integers x,yx,y with ax+ny=1ax + ny = 1. This says ax−1=−nyax - 1 = -ny, that is, ax≡1(modn)ax \equiv 1 \pmod n, so [a][x]=[1][a][x] = [1].

(⇒)(\Rightarrow) If [a][b]=[1][a][b] = [1], then ab−1=nkab - 1 = nk for some integer kk, so ab−nk=1ab - nk = 1. Now d=gcd⁡(a,n)d = \gcd(a,n) divides both terms on the left, so d∣1d \mid 1 and therefore d=1d = 1.

Let us check the group axioms. The class [1][1] is a unit. If [a][a] and [b][b] are units with [a][x]=[1][a][x]=[1] and [b][y]=[1][b][y]=[1], then [ab][xy]=[a][x][b][y]=[1][ab][xy] = [a][x][b][y] = [1], so the product [ab][ab] is a unit and the operation is closed. Associativity comes from multiplication in the ring, and the existence of inverses is the definition of a unit.

Corollary 4.5

The ring Z/nZ\mathbb{Z}/n\mathbb{Z} is a field if and only if nn is prime.

Proof(Corollary 4.5)

Let n=pn = p be prime. Saying [a]≠[0][a] \ne [0] means p∤ap \nmid a, and since pp is prime, gcd⁡(a,p)∈{1,p}\gcd(a,p) \in \{1,p\} with pp excluded, so gcd⁡(a,p)=1\gcd(a,p)=1. By Proposition 4.4, [a][a] is a unit, so Z/pZ\mathbb{Z}/p\mathbb{Z} is a field (and p≥2p \ge 2 ensures [1]≠[0][1] \ne [0]).

Conversely, if nn is composite then n=abn = ab with 1<a,b<n1 < a, b < n, and [a]≠[0][a] \ne [0], [b]≠[0][b] \ne [0] while [a][b]=[n]=[0][a][b] = [n] = [0]. A field has no zero divisors (if [a][b]=[0][a][b]=[0] and [a][a] is invertible, multiplying both sides by [a]−1[a]^{-1} gives [b]=[0][b]=[0]), so this is not a field. For n=1n = 1 we have [1]=[0][1]=[0], which violates the definition of a field.

For a prime pp we write Fp\mathbb{F}_p for the field Z/pZ\mathbb{Z}/p\mathbb{Z}. What Corollary 4.5 says is that the primes are the only moduli producing a “finite world in which one can divide”. The special status of primes takes an algebraic form here. The same statement is obtained from the side of ring theory as Corollary 5.5[環と体の基礎].

Definition 4.6(Euler's totient function)

For n∈Nn \in \mathbb{N} set

φ(n):=#{a∈Z∣1≤a≤n, gcd⁡(a,n)=1}.\varphi(n) := \#\{a \in \mathbb{Z} \mid 1 \le a \le n,\ \gcd(a,n)=1\} .

By Proposition 4.4 we have φ(n)=#(Z/nZ)×\varphi(n) = \#(\mathbb{Z}/n\mathbb{Z})^{\times}.

Example 4.7(The group of units of Z/12Z)

Among 11 through 1212, those coprime to 1212 are 1,5,7,111, 5, 7, 11, four in all, so φ(12)=4\varphi(12)=4 and (Z/12Z)×={[1],[5],[7],[11]}(\mathbb{Z}/12\mathbb{Z})^{\times} = \{[1],[5],[7],[11]\}. Writing out the multiplication table,

[5]2=[25]=[1],[7]2=[49]=[1],[11]2=[121]=[1],[5][7]=[35]=[11],[5]^2 = [25] = [1], \quad [7]^2 = [49] = [1], \quad [11]^2 = [121] = [1], \quad [5][7] = [35] = [11] ,

so every element other than the identity has order 22. This group is therefore isomorphic to the Klein four-group Z/2Z×Z/2Z\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2\mathbb{Z} and is not cyclic. By contrast, it is known that for a prime pp the group (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^{\times} is always cyclic (existence of a primitive root).

Theorem 4.8(Chinese remainder theorem)

Let m,n∈Nm, n \in \mathbb{N} with gcd⁡(m,n)=1\gcd(m,n)=1. Then the map

Φ:Z/mnZ⟶Z/mZ×Z/nZ,[a]mn⟼([a]m,[a]n)\Phi : \mathbb{Z}/mn\mathbb{Z} \longrightarrow \mathbb{Z}/m\mathbb{Z} \times \mathbb{Z}/n\mathbb{Z}, \qquad [a]_{mn} \longmapsto ([a]_m, [a]_n)

is a well-defined ring isomorphism. In particular, for any integers r,sr, s the simultaneous congruences x≡r(modm)x \equiv r \pmod m and x≡s(modn)x \equiv s \pmod n have a unique solution modulo mnmn.

Proof(Theorem 4.8)

Well-definedness: if [a]mn=[a′]mn[a]_{mn} = [a']_{mn} then mn∣(a−a′)mn \mid (a-a'), hence in particular m∣(a−a′)m \mid (a-a') and n∣(a−a′)n \mid (a-a'), so ([a]m,[a]n)=([a′]m,[a′]n)([a]_m,[a]_n) = ([a']_m,[a']_n). That Φ\Phi is a ring homomorphism follows at once, since addition and multiplication in each component are exactly the operations of Definition 4.1; and Φ([1]mn)=([1]m,[1]n)\Phi([1]_{mn}) = ([1]_m,[1]_n) is clear.

Injectivity: suppose Φ([a]mn)=([0]m,[0]n)\Phi([a]_{mn}) = ([0]_m,[0]_n), so m∣am \mid a and n∣an \mid a. Writing a=mka = mk we get n∣mkn \mid mk, and since gcd⁡(n,m)=1\gcd(n,m)=1, Lemma 2.3 gives n∣kn \mid k, say k=nlk = nl. Hence a=mnla = mnl, that is, [a]mn=[0]mn[a]_{mn} = [0]_{mn}. Since the kernel of the homomorphism is trivial, Φ\Phi is injective.

Surjectivity: the domain and codomain are both finite, with mnmn and m⋅nm \cdot n elements respectively, hence of equal size. An injective map between finite sets of equal cardinality is surjective, so Φ\Phi is a bijection.

The final assertion is exactly the bijectivity of Φ\Phi: the preimage of ([r]m,[s]n)([r]_m,[s]_n) is a single residue class [x]mn[x]_{mn}, which means existence of a solution together with uniqueness modulo mnmn.

Corollary 4.9

If gcd⁡(m,n)=1\gcd(m,n)=1 then φ(mn)=φ(m)φ(n)\varphi(mn) = \varphi(m)\varphi(n). Moreover, if n=p1e1⋯pkekn = p_1^{e_1}\cdots p_k^{e_k} is the prime factorization of nn, then

φ(n)=n∏i=1k(1−1pi).\varphi(n) = n \prod_{i=1}^{k}\left(1 - \frac{1}{p_i}\right).
Proof(Corollary 4.9)

A ring isomorphism carries units to units and non-units to non-units (since Φ(u)Φ(u−1)=Φ(1)=1\Phi(u)\Phi(u^{-1}) = \Phi(1) = 1, and likewise in the other direction for Φ−1\Phi^{-1}). Hence the map Φ\Phi of Theorem 4.8 induces a bijection between groups of units,

(Z/mnZ)×→ ∼ (Z/mZ)××(Z/nZ)×.(\mathbb{Z}/mn\mathbb{Z})^{\times} \xrightarrow{\ \sim\ } (\mathbb{Z}/m\mathbb{Z})^{\times} \times (\mathbb{Z}/n\mathbb{Z})^{\times} .

Counting elements on both sides gives φ(mn)=φ(m)φ(n)\varphi(mn)=\varphi(m)\varphi(n).

For a prime power pep^e, the integers from 11 to pep^e that are not coprime to pep^e are the multiples of pp, namely p,2p,…,pe−1⋅pp, 2p, \ldots, p^{e-1}\cdot p, of which there are pe−1p^{e-1}. Hence

φ(pe)=pe−pe−1=pe(1−1p).\varphi(p^e) = p^e - p^{e-1} = p^e\left(1 - \frac{1}{p}\right).

Distinct prime powers are coprime, so repeated use of multiplicativity gives the formula.

5. Fermat’s little theorem and Euler’s theorem

Section titled “5. Fermat’s little theorem and Euler’s theorem”

Fermat stated this theorem in a letter to Frénicle in 1640, but omitted the proof, saying it would run too long. The first published proof is due to Euler (1736).

Theorem 5.1(Fermat's little theorem)

Let pp be prime and let aa be an integer with p∤ap \nmid a. Then

ap−1≡1(modp).a^{p-1} \equiv 1 \pmod p .
Proof(Theorem 5.1)

Let S={1,2,…,p−1}S = \{1, 2, \ldots, p-1\} and define a map σ\sigma by sending x∈Sx \in S to ax mod pax \bmod p. We argue in three steps.

Step one (the values of σ\sigma lie in SS). From 1≤x≤p−11 \le x \le p-1 and p∤ap \nmid a, the second half of Lemma 2.3 gives p∤axp \nmid ax. Hence ax mod p≠0ax \bmod p \ne 0, that is, σ(x)∈S\sigma(x) \in S.

Step two (σ\sigma is injective). Suppose σ(x)=σ(y)\sigma(x) = \sigma(y), so ax≡ay(modp)ax \equiv ay \pmod p. From p∤ap \nmid a we get gcd⁡(a,p)=1\gcd(a,p)=1, so applying Proposition 3.6 with c=ac=a, n=pn=p, d=1d=1 gives x≡y(modp)x \equiv y \pmod p. Since xx and yy both lie between 11 and p−1p-1, we have ∣x−y∣<p|x-y| < p and hence x=yx = y. An injective map from the finite set SS to itself is a bijection, so σ\sigma is a permutation of SS.

Step three (comparing products). Since σ\sigma is a permutation,

∏x∈Sσ(x)=∏x∈Sx=(p−1)!.\prod_{x \in S} \sigma(x) = \prod_{x \in S} x = (p-1)! .

On the other hand σ(x)≡ax(modp)\sigma(x) \equiv ax \pmod p for each xx, so applying Proposition 3.3 p−1p-1 times,

∏x∈Sσ(x)≡∏x∈S(ax)=ap−1(p−1)!(modp).\prod_{x \in S}\sigma(x) \equiv \prod_{x\in S} (ax) = a^{p-1}(p-1)! \pmod p .

Combining the two gives ap−1(p−1)!≡(p−1)!(modp)a^{p-1}(p-1)! \equiv (p-1)! \pmod p. None of the xx with 1≤x≤p−11 \le x \le p-1 is divisible by pp, so repeated use of Lemma 2.3 gives p∤(p−1)!p \nmid (p-1)!, that is, gcd⁡((p−1)!,p)=1\gcd((p-1)!,p)=1. Hence Proposition 3.6 lets us cancel (p−1)!(p-1)!, and we obtain ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p.

Corollary 5.2

Let pp be prime. Then ap≡a(modp)a^{p} \equiv a \pmod p for every integer aa.

Proof(Corollary 5.2)

If p∤ap \nmid a, multiply both sides of Theorem 5.1 by aa to get ap≡a(modp)a^p \equiv a \pmod p. If p∣ap \mid a, then a≡0(modp)a \equiv 0 \pmod p, so Proposition 3.3 gives ap≡0p=0≡a(modp)a^p \equiv 0^p = 0 \equiv a \pmod p. In either case the congruence holds.

The form in Corollary 5.2 imposes no condition on aa, which makes it convenient; it will do the work later when we prove the correctness of RSA.

Theorem 5.3(Euler's theorem)

Let n∈Nn \in \mathbb{N} and a∈Za \in \mathbb{Z} with gcd⁡(a,n)=1\gcd(a,n)=1. Then

aφ(n)≡1(modn).a^{\varphi(n)} \equiv 1 \pmod n .
Proof(Theorem 5.3)

We lift the proof of Theorem 5.1 verbatim. Put U=(Z/nZ)×U = (\mathbb{Z}/n\mathbb{Z})^{\times}, so that #U=φ(n)\#U = \varphi(n) (Definition 4.6). Since gcd⁡(a,n)=1\gcd(a,n)=1, Proposition 4.4 gives [a]∈U[a] \in U.

Consider the map μ[a]:U→U\mu_{[a]} : U \to U, [x]↦[a][x][x] \mapsto [a][x]. Because UU is a group (Proposition 4.4), we have [a][x]∈U[a][x] \in U, so μ[a]\mu_{[a]} really does map UU into UU. Moreover μ[a]−1\mu_{[a]^{-1}} is an inverse map, so μ[a]\mu_{[a]} is a bijection, that is, a permutation of UU.

Now compute the product P=∏[x]∈U[x]P = \prod_{[x]\in U}[x] of all elements of UU in two ways. Reordering by a permutation does not change the product, so

P=∏[x]∈Uμ[a]([x])=∏[x]∈U[a][x]=[a]φ(n)P.P = \prod_{[x]\in U} \mu_{[a]}([x]) = \prod_{[x]\in U} [a][x] = [a]^{\varphi(n)} P .

Being a product of units, PP is itself a unit (by the group property in Proposition 4.4), so multiplying both sides by P−1P^{-1} gives [a]φ(n)=[1][a]^{\varphi(n)} = [1], that is, aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod n.

Remark 5.4

In the language of group theory, Theorem 5.3 is a one-liner: it is just the corollary of Lagrange’s theorem stating that g#G=eg^{\#G} = e for an element gg of a finite group GG, applied to G=(Z/nZ)×G = (\mathbb{Z}/n\mathbb{Z})^{\times}. For details see Theorem 7.3[Subgroups and Cosets] in Subgroups and cosets (Lagrange’s theorem). The proof above amounts to writing out that special case without group-theoretic language. Historically it came first; the notion of a group was distilled from an accumulation of such concrete examples (Introduction to group theory — the definition and examples).

Proposition 5.5

Let pp be prime and p∤ap \nmid a. Then the inverse of [a][a] in Z/pZ\mathbb{Z}/p\mathbb{Z} is given by [ap−2][a^{p-2}].

Proof(Proposition 5.5)

Since p≥2p \ge 2 we have p−2≥0p - 2 \ge 0, so ap−2a^{p-2} is an integer. By Theorem 5.1,

[a]⋅[ap−2]=[ap−1]=[1].[a] \cdot [a^{p-2}] = [a^{p-1}] = [1] .

The inverse is unique (by general group theory, or because if [b][b] and [b′][b'] are both inverses then [b]=[b][a][b′]=[b′][b] = [b][a][b'] = [b']), so this is [a]−1[a]^{-1}.

Example 5.6(3 to the 1000th power modulo 7)

Since 77 is prime and 7∤37 \nmid 3, Theorem 5.1 gives 36≡1(mod7)3^6 \equiv 1 \pmod 7. As 1000=6⋅166+41000 = 6 \cdot 166 + 4,

31000=(36)166⋅34≡1166⋅34=81(mod7).3^{1000} = (3^{6})^{166} \cdot 3^{4} \equiv 1^{166}\cdot 3^4 = 81 \pmod 7 .

Since 81=7⋅11+481 = 7\cdot 11 + 4, we get 31000≡4(mod7)3^{1000} \equiv 4 \pmod 7. Replacing the exponent by its remainder modulo φ(7)=6\varphi(7)=6 is the practical use of the little theorem.

6. Applications — fast exponentiation, RSA, primality testing

Section titled “6. Applications — fast exponentiation, RSA, primality testing”

Computing ae mod na^e \bmod n does not require ee multiplications. Expand ee in binary and square repeatedly to get a,a2,a4,…a, a^2, a^4, \ldots, multiplying in only the terms needed; then O(log⁡e)O(\log e) multiplications suffice. Since we reduce  mod  n\bmod\ n at every stage, no intermediate number ever exceeds n2n^2 (the justification is Proposition 3.3).

def power_mod(a, e, n):
"""Compute a^e mod n by repeated squaring. Requires e >= 0, n >= 1."""
result = 1
a %= n
while e > 0:
if e & 1:
result = result * a % n
a = a * a % n
e >>= 1
return result
assert power_mod(2, 100, 100) == 76 # last two digits of 2^100
assert power_mod(3, 1000, 7) == 4 # 3^1000 modulo 7

To compute inverses we use the extended Euclidean algorithm, which is the proof of Theorem 2.2 turned into a procedure.

def ext_gcd(a, b):
"""Return (g, x, y) with g = gcd(a, b) and a*x + b*y = g."""
if b == 0:
return (a, 1, 0)
g, x, y = ext_gcd(b, a % b)
return (g, y, x - (a // b) * y)
def inverse_mod(a, n):
g, x, _ = ext_gcd(a % n, n)
if g != 1:
raise ValueError("the inverse does not exist")
return x % n
assert inverse_mod(7, 120) == 103

Theorem 6.1(Correctness of RSA)

Let p≠qp \ne q be primes, and set n=pqn = pq and φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1). Suppose integers e,de, d satisfy

ed≡1(modφ(n)),gcd⁡(e,φ(n))=1.ed \equiv 1 \pmod{\varphi(n)}, \qquad \gcd(e,\varphi(n)) = 1 .

Then for every integer mm,

(me)d≡m(modn).(m^{e})^{d} \equiv m \pmod n .
Proof(Theorem 6.1)

By hypothesis there is an integer k≥0k \ge 0 with ed=1+kφ(n)=1+k(p−1)(q−1)ed = 1 + k\varphi(n) = 1 + k(p-1)(q-1) (we may assume ed≥1ed \ge 1).

We first show med≡m(modp)m^{ed} \equiv m \pmod p. If p∣mp \mid m then both sides are congruent to 00 and the claim holds. If p∤mp \nmid m, then Theorem 5.1 gives mp−1≡1(modp)m^{p-1}\equiv 1 \pmod p, so

med=m⋅(mp−1)k(q−1)≡m⋅1k(q−1)=m(modp),m^{ed} = m \cdot \left(m^{p-1}\right)^{k(q-1)} \equiv m \cdot 1^{k(q-1)} = m \pmod p ,

where we used the multiplicative and power statements of Proposition 3.3. Interchanging pp and qq, the same argument gives med≡m(modq)m^{ed}\equiv m \pmod q.

Hence p∣(med−m)p \mid (m^{ed}-m) and q∣(med−m)q \mid (m^{ed}-m). Since p≠qp \ne q are both prime, gcd⁡(p,q)=1\gcd(p,q)=1, so the injectivity argument of Theorem 4.8 (or Lemma 2.3 directly) gives pq∣(med−m)pq \mid (m^{ed}-m), that is, med≡m(modn)m^{ed}\equiv m \pmod n.

It matters that we did not assume gcd⁡(m,n)=1\gcd(m,n)=1. Using Theorem 5.3 as it stands would require that hypothesis, but splitting into prime factors and using the form Corollary 5.2 dispenses with it.

Key generation, encryption, and decryption in RSA. Security rests on the difficulty of recovering p and q from n.

Example 6.2(Carrying RSA through with a small key)

Take p=11p = 11 and q=13q = 13, so n=143n = 143 and φ(n)=10⋅12=120\varphi(n) = 10 \cdot 12 = 120. The exponent e=7e = 7 satisfies gcd⁡(7,120)=1\gcd(7,120)=1. Then dd solves 7d≡1(mod120)7d \equiv 1 \pmod{120}, and from 7⋅103=721=6⋅120+17 \cdot 103 = 721 = 6\cdot 120 + 1 we get d=103d = 103.

Encrypt the plaintext m=5m = 5:

52=25,54≡252=625=4⋅143+53≡53(mod143),5^2 = 25,\quad 5^4 \equiv 25^2 = 625 = 4\cdot 143 + 53 \equiv 53 \pmod{143},c=57=54⋅52⋅5≡53⋅25⋅5(mod143).c = 5^7 = 5^4\cdot 5^2 \cdot 5 \equiv 53 \cdot 25 \cdot 5 \pmod{143}.

Since 53⋅25=1325=9⋅143+38≡3853\cdot 25 = 1325 = 9\cdot 143 + 38 \equiv 38 and 38⋅5=190=143+47≡4738 \cdot 5 = 190 = 143 + 47 \equiv 47, we get c=47c = 47.

Now decrypt. Instead of computing 47103 mod 14347^{103} \bmod 143 directly, we use Theorem 4.8 and split into the moduli 1111 and 1313.

Modulo 1111: 47=4⋅11+3≡347 = 4\cdot 11 + 3 \equiv 3, and Theorem 5.1 gives 310≡1(mod11)3^{10}\equiv 1 \pmod{11}; since 103=10⋅10+3103 = 10\cdot 10 + 3, we get 47103≡33=27≡5(mod11)47^{103} \equiv 3^{3} = 27 \equiv 5 \pmod{11}.

Modulo 1313: 47=3⋅13+8≡847 = 3\cdot 13 + 8 \equiv 8 and 812≡1(mod13)8^{12}\equiv 1 \pmod{13}; since 103=12⋅8+7103 = 12\cdot 8 + 7, we get 47103≡87(mod13)47^{103}\equiv 8^{7} \pmod{13}. Here 82=64=4⋅13+12≡−18^2 = 64 = 4\cdot 13 + 12 \equiv -1, so 87=(82)3⋅8≡(−1)3⋅8=−8≡5(mod13)8^7 = (8^2)^3 \cdot 8 \equiv (-1)^3\cdot 8 = -8 \equiv 5 \pmod{13}.

The unique solution modulo 143143 of x≡5(mod11)x \equiv 5 \pmod{11} and x≡5(mod13)x \equiv 5 \pmod{13} is x=5x = 5. The plaintext m=5m=5 has indeed been recovered.

6.3. The Fermat primality test and Carmichael numbers

Section titled “6.3. The Fermat primality test and Carmichael numbers”

The contrapositive of Corollary 5.2 can be used to test primality: if an−1≢1(modn)a^{n-1}\not\equiv 1 \pmod n for some aa (with gcd⁡(a,n)=1\gcd(a,n)=1), then nn is not prime. This test runs quickly using repeated squaring.

The trouble is the converse. The fact that an−1≡1(modn)a^{n-1}\equiv 1 \pmod n holds does not make nn prime.

Definition 6.3(Carmichael number)

A composite number nn satisfying an−1≡1(modn)a^{n-1}\equiv 1 \pmod n for every integer aa with gcd⁡(a,n)=1\gcd(a,n)=1 is called a Carmichael number.

Example 6.4(561 is a Carmichael number)

We have 561=3⋅11⋅17561 = 3\cdot 11\cdot 17, which is composite. Also 560560 is divisible by each of 2,10,162, 10, 16 (indeed 560=2⋅280=10⋅56=16⋅35560 = 2\cdot 280 = 10\cdot 56 = 16\cdot 35). If gcd⁡(a,561)=1\gcd(a,561)=1, then aa is divisible by none of 3,11,173, 11, 17, so Theorem 5.1 gives

a2≡1(mod3),a10≡1(mod11),a16≡1(mod17).a^{2}\equiv 1 \pmod 3, \qquad a^{10}\equiv 1\pmod{11}, \qquad a^{16}\equiv 1 \pmod{17}.

Since 560560 is a multiple of each of these exponents, we get a560=(a2)280≡1(mod3)a^{560} = (a^{2})^{280} \equiv 1 \pmod 3 and similarly for the others, so a560≡1a^{560}\equiv 1 modulo all three primes. Using Theorem 4.8 twice yields 561∣(a560−1)561 \mid (a^{560}-1), that is, a560≡1(mod561)a^{560}\equiv 1 \pmod{561}.

Here 561561 is the smallest Carmichael number. In 1994 Alford, Granville, and Pomerance proved that there are infinitely many Carmichael numbers.

The Fermat test is therefore not a deterministic primality test. The defect is remedied by the Miller–Rabin test, which writes n−1=2stn-1 = 2^{s}t with tt odd and inspects the sequence at,a2t,…a^{t}, a^{2t},\ldots to detect the appearance of a nontrivial square root of 11. The principle is that if Z/pZ\mathbb{Z}/p\mathbb{Z} is a field (Corollary 4.5), the solutions of x2=1x^2 = 1 are only ±1\pm 1. A composite number slips past the Miller–Rabin test with probability at most 1/41/4 for each base, so taking many bases gives certainty that is ample in practice.

Remark 6.5

The ring Z/nZ\mathbb{Z}/n\mathbb{Z} treated in this article is the first step in the practice of studying arithmetic objects by mapping them into finite algebraic systems. Pushing the same idea further leads to the procedure of counting, for each prime pp, the number of solutions of an equation over Fp\mathbb{F}_p and assembling these counts into a generating function. Carrying this out for elliptic curves gives the Hasse–Weil LL-function, the subject of Elliptic curves and modular forms and Fermat’s Last Theorem. The names are similar, but this is the distance separating Fermat’s little theorem from his last theorem.

Exercise 7.1Easy

Find the units digit of 720267^{2026}.

Solution

The units digit is the residue modulo 1010. We have gcd⁡(7,10)=1\gcd(7,10)=1 and φ(10)=φ(2)φ(5)=1⋅4=4\varphi(10)=\varphi(2)\varphi(5)=1\cdot 4=4 (Corollary 4.9), so Theorem 5.3 gives 74≡1(mod10)7^{4}\equiv 1 \pmod{10}. Since 2026=4⋅506+22026 = 4\cdot 506 + 2,

72026=(74)506⋅72≡1506⋅49≡9(mod10).7^{2026} = (7^{4})^{506}\cdot 7^{2} \equiv 1^{506}\cdot 49 \equiv 9 \pmod{10}.

Hence the units digit is 99.

Exercise 7.2Standard

Prove that (p−1)!≡−1(modp)(p-1)! \equiv -1 \pmod p for every prime pp (Wilson’s theorem).

Solution

For p=2p = 2 we have 1!=1≡−1(mod2)1! = 1 \equiv -1 \pmod 2, so the claim holds; assume from now on that pp is an odd prime.

Since Z/pZ\mathbb{Z}/p\mathbb{Z} is a field (Corollary 4.5), all of [1],…,[p−1][1],\ldots,[p-1] are invertible. We first determine the elements that are their own inverses. The equation [x]2=[1][x]^2=[1] is equivalent to p∣(x−1)(x+1)p \mid (x-1)(x+1), and Lemma 2.3 gives p∣(x−1)p \mid (x-1) or p∣(x+1)p \mid (x+1), that is, x≡1x \equiv 1 or x≡−1(modp)x\equiv -1 \pmod p. For an odd prime pp we have 1≢−11 \not\equiv -1, so there are exactly two self-inverse elements, [1][1] and [p−1][p-1].

The remaining p−3p-3 elements split into pairs {[x],[x]−1}\{[x], [x]^{-1}\} of distinct elements (uniqueness of inverses makes this pairing consistent). The product of each pair is [1][1], so

(p−1)!=∏x=1p−1[x]=[1]⋅[p−1]⋅∏pairs[1]=[p−1]=[−1].(p-1)! = \prod_{x=1}^{p-1}[x] = [1]\cdot [p-1]\cdot \prod_{\text{pairs}} [1] = [p-1] = [-1].

Hence (p−1)!≡−1(modp)(p-1)!\equiv -1 \pmod p.

Exercise 7.3Standard

For 341=11⋅31341 = 11\cdot 31, show that 2340≡1(mod341)2^{340}\equiv 1 \pmod{341} (so that 341341 is a Fermat pseudoprime to the base 22). Also compute 3340 mod 3413^{340} \bmod 341 and confirm that the base 33 does detect 341341 as composite.

Solution

Base 22: from 210=1024=93⋅11+12^{10}=1024 = 93\cdot 11 + 1 we get 210≡1(mod11)2^{10}\equiv 1 \pmod{11}, and since 340=10⋅34340 = 10\cdot 34, 2340≡1(mod11)2^{340}\equiv 1 \pmod{11}. Also 25=32=31+1≡1(mod31)2^{5}=32 = 31+1 \equiv 1 \pmod{31} and 340=5⋅68340 = 5\cdot 68, so 2340≡1(mod31)2^{340}\equiv 1 \pmod{31}. Since gcd⁡(11,31)=1\gcd(11,31)=1, Theorem 4.8 gives 341∣(2340−1)341 \mid (2^{340}-1), that is, 2340≡1(mod341)2^{340}\equiv 1\pmod{341}.

Base 33: modulo 1111 we have 35=243=22⋅11+1≡13^{5}=243 = 22\cdot 11 + 1 \equiv 1 and 340=5⋅68340 = 5\cdot 68, so 3340≡1(mod11)3^{340}\equiv 1 \pmod{11}. Modulo 3131, Theorem 5.1 gives 330≡13^{30}\equiv 1, and since 340=30⋅11+10340 = 30\cdot 11 + 10 we get 3340≡310(mod31)3^{340}\equiv 3^{10} \pmod{31}. Here 33=273^{3}=27 and 35=243=7⋅31+26≡−53^{5}=243 = 7\cdot 31 + 26 \equiv -5, so 310≡(−5)2=25(mod31)3^{10} \equiv (-5)^2 = 25 \pmod{31}.

Since 3340≡25≢1(mod31)3^{340}\equiv 25 \not\equiv 1 \pmod{31}, we have 3340≢1(mod341)3^{340}\not\equiv 1 \pmod{341}, and the contrapositive of Corollary 5.2 shows that 341341 is composite. To find the value, solve x≡1(mod11)x\equiv 1 \pmod{11} and x≡25(mod31)x \equiv 25 \pmod{31}. Putting x=25+31kx = 25 + 31k and using 25≡325 \equiv 3, 31≡9(mod11)31\equiv 9 \pmod{11}, we need 3+9k≡13 + 9k \equiv 1, that is, 9k≡−2≡9(mod11)9k \equiv -2 \equiv 9 \pmod{11}; since gcd⁡(9,11)=1\gcd(9,11)=1, Proposition 3.6 gives k≡1(mod11)k \equiv 1 \pmod{11}. Taking k=1k=1 gives x=56x = 56, so 3340≡56(mod341)3^{340}\equiv 56 \pmod{341}.

Exercise 7.4Hard

Show that if a composite number nn is a Carmichael number (Definition 6.3), then nn is squarefree and (p−1)∣(n−1)(p-1)\mid (n-1) for every prime factor pp of nn (the necessity half of Korselt’s criterion).

Solution

Let nn be a Carmichael number.

Squarefreeness. Suppose there were a prime pp with p2∣np^{2}\mid n. From the prime factorization of nn we may write n=pemn = p^{e}m with e≥2e \ge 2 and p∤mp \nmid m. Since gcd⁡(p2,m)=1\gcd(p^{2}, m)=1, Theorem 4.8 supplies an integer aa with

a≡1+p(modp2),a≡1(modm).a \equiv 1 + p \pmod{p^{2}}, \qquad a \equiv 1 \pmod{m} .

This aa is not divisible by pp (because a≡1(modp)a \equiv 1 \pmod p) and is divisible by no prime factor of mm (because a≡1(modm)a\equiv 1 \pmod m), so gcd⁡(a,n)=1\gcd(a,n)=1.

By the binomial theorem, modulo p2p^{2},

ak≡(1+p)k≡1+kp(modp2)a^{k} \equiv (1+p)^{k} \equiv 1 + kp \pmod{p^{2}}

(every term of degree at least 22 in pp is a multiple of p2p^{2}). Hence an−1≡1(modp2)a^{n-1}\equiv 1 \pmod{p^{2}} is equivalent to p2∣(n−1)pp^{2} \mid (n-1)p, that is, to p∣(n−1)p \mid (n-1). But p∣np \mid n, so p∤(n−1)p \nmid (n-1) — a contradiction. Therefore nn is squarefree.

The divisibility (p−1)∣(n−1)(p-1)\mid(n-1). Let pp be a prime factor of nn. The ring Z/pZ\mathbb{Z}/p\mathbb{Z} is a field and its group of units (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^{\times} is cyclic of order p−1p-1. Take a generator gg, and use Theorem 4.8 to obtain aa with a≡g(modp)a \equiv g \pmod p and a≡1(modn/p)a \equiv 1 \pmod{n/p}; then gcd⁡(a,n)=1\gcd(a,n)=1 (since nn is squarefree we may use gcd⁡(p,n/p)=1\gcd(p, n/p)=1).

By the definition of a Carmichael number, an−1≡1(modn)a^{n-1}\equiv 1 \pmod n, and in particular an−1≡1(modp)a^{n-1}\equiv 1 \pmod p. That is, gn−1=1g^{n-1} = 1 holds in (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^{\times}. The order of gg is p−1p-1, so the only kk with gk=1g^{k}=1 are the multiples of p−1p-1. Hence (p−1)∣(n−1)(p-1)\mid(n-1).

(For 561561, that 3−1=23-1=2, 11−1=1011-1=10, and 17−1=1617-1=16 all divide 560560 was checked in Example 6.4.)

  • Teiji Takagi, Shotō Seisūron Kōgi, 2nd ed., Kyoritsu Shuppan, 1971 (in Japanese) — Chapters 1 and 2. A classic, available in Japanese, running from the introduction of congruences through Euler’s theorem to primitive roots.
  • G. H. Hardy and E. M. Wright, An Introduction to the Theory of Numbers, 6th ed., Oxford University Press, 2008 — Chapters 5 and 6 (congruences and Fermat’s theorem).
  • K. Ireland and M. Rosen, A Classical Introduction to Modern Number Theory, 2nd ed., Springer GTM 84, 1990 — Chapters 3 and 4, treating the structure of residue rings and their unit groups algebraically.
  • C. F. Gauss, Disquisitiones Arithmeticae, 1801 (English translation: Springer, 1986) — Sections 1 and 2 introduce the congruence symbol ≡\equiv and its basic properties.
  • R. L. Rivest, A. Shamir, L. Adleman, “A Method for Obtaining Digital Signatures and Public-Key Cryptosystems”, Communications of the ACM 21 (1978), 120–126. DOI: 10.1145/359340.359342
  • W. R. Alford, A. Granville, C. Pomerance, “There are Infinitely Many Carmichael Numbers”, Annals of Mathematics 139 (1994), 703–722.
  • Akihiko Yukie, Daisūgaku 1: Gunron Nyūmon, Nippon Hyoron Sha, 2010 (in Japanese) — Chapter 2, where the route from Lagrange’s theorem to Theorem 5.3 is set out.

Appendix: A combinatorial proof of Fermat’s little theorem

Section titled “Appendix: A combinatorial proof of Fermat’s little theorem”

Setting up the objects to be counted. There is a proof of Theorem 5.1 that uses no residue classes at all. Let a≥1a \ge 1 be an integer, pp a prime, and let XX be the set of all strings of pp beads in a row, each bead having one of aa colours. Then #X=ap\#X = a^{p}.

Separating monochromatic strings from the rest. Let the “cyclic shift” act on XX: the operation τ\tau sending a string (x1,…,xp)(x_1,\ldots,x_p) to (x2,…,xp,x1)(x_2,\ldots,x_p,x_1). Since τp\tau^{p} is the identity, XX is partitioned into the orbits of τ\tau. If the orbit of some string has size kk, then kk is the least positive integer with τk\tau^{k} fixing that string, and since τp\tau^{p} also fixes it, k∣pk \mid p. As pp is prime, either k=1k = 1 or k=pk = p.

An orbit has size 1 only for monochromatic strings. Saying k=1k=1 means invariance under τ\tau, that is, x1=x2=⋯=xpx_1 = x_2 = \cdots = x_p. There are as many such strings as there are colours, namely aa. The remaining ap−aa^{p}-a strings all lie in orbits of size pp.

The conclusion of the count. The set obtained from XX by removing the monochromatic strings is a disjoint union of orbits of size pp, so its cardinality is a multiple of pp. That is,

p∣(ap−a),p \mid (a^{p}-a) ,

which is nothing other than Corollary 5.2. When p∤ap \nmid a we have gcd⁡(a,p)=1\gcd(a,p)=1, so Proposition 3.6 lets us cancel aa and obtain ap−1≡1(modp)a^{p-1}\equiv 1 \pmod p. For aa negative or zero, replace aa by its remainder on division by pp; Proposition 3.3 then gives the same conclusion.

What this proof means. The essential point is that it explains an arithmetic fact, “divisibility by pp”, as a fact about counting: “the objects can be sorted into groups of pp”. It is also the simplest manifestation of the general theory of orbits and fixed points, in which the group Z/pZ\mathbb{Z}/p\mathbb{Z} acts on a set and the size of each orbit divides the order of the group (Theorem 6.1[Subgroups and Cosets]).

not audited

Report an error in this article ・Operated by: Mugen Giken LLC ・Pricing ・Terms ・Legal notice

© 2026 夢現技研合同会社 ・Code samples are MIT licensed.