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 ab(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 ap11(modp)a^{p-1} \equiv 1 \pmod p (for pap \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/mnZZ/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×32×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 aba \mid b for ”aa divides bb”, that is, for the existence of some cZc \in \mathbb{Z} with b=acb = ac.

Theorem 2.1Division algorithm

Let aZa \in \mathbb{Z} and nNn \in \mathbb{N}. Then there is exactly one pair of integers q,rq, r with

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

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

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

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

We write this rr as amodna \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.2Bé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+byx,yZ}\{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+byx,yZ}I = \{ax + by \mid x, y \in \mathbb{Z}\} and let I+I^{+} be the set of positive integers in II. If a0a \ne 0 then aa+b0=a2>0a \cdot a + b \cdot 0 = a^2 > 0 lies in I+I^{+}; if a=0a = 0 then b0b \ne 0, so b2I+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 dad \mid a. By Theorem 2.1 write a=qd+ra = qd + r with 0r<d0 \le r < d; then

r=aqd=aq(ax0+by0)=a(1qx0)+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 rI+r \in I^{+} and r<dr < d, contradicting the minimality of dd. Hence r=0r = 0, that is, dad \mid a. The same argument gives dbd \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 cdc \mid d and therefore cdc \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 dad \mid a and dbd \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.3Euclid's lemma

If gcd(c,n)=1\gcd(c,n) = 1 and ncmn \mid cm, then nmn \mid m. In particular, if pp is prime and pabp \mid ab, then pap \mid a or pbp \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 ncmn \mid cm, so the first term on the right is divisible by nn, and the second term is plainly a multiple of nn. Hence nmn \mid m.

For the second statement, suppose pap \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 pbp \mid b.

Definition 3.1Congruence

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

ab(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,,n10, 1, \ldots, n-1.

Proof(Proposition 3.2)

Reflexivity holds because n0=aan \mid 0 = a - a. Symmetry follows because if n(ab)n \mid (a-b) then ba=(ab)b - a = -(a-b) is also a multiple of nn. For transitivity, if ab=nka - b = nk and bc=nlb - c = nl then ac=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 0r<n0 \le r < n, so ar=qna - r = qn, that is, ar(modn)a \equiv r \pmod n. Hence each class contains one of 0,,n10,\ldots,n-1. Moreover if 0r<r<n0 \le r < r' < n then 0<rr<n0 < r' - r < n, so n(rr)n \nmid (r' - r) and rr is not congruent to rr'. 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.3Arithmetic of congruences

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

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

In particular akbk(modn)a^k \equiv b^k \pmod n for every kNk \in \mathbb{N}.

Proof(Proposition 3.3)

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

(a+c)(b+d)=(ab)+(cd)=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,

acbd=acbc+bcbd=c(ab)+b(cd)=n(cs+bt),ac - bd = ac - bc + bc - bd = c(a-b) + b(c-d) = n(cs + bt) ,

so n(acbd)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 akbka^k \equiv b^k, applying the multiplicative statement to this and to aba \equiv b gives ak+1bk+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.4The last two digits of 2 to the 100th power

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

220242=57676(mod100).2^{20} \equiv 24^2 = 576 \equiv 76 \pmod{100}.

Furthermore 762=577676(mod100)76^2 = 5776 \equiv 76 \pmod{100}, so 240762^{40} \equiv 76 and 280762^{80} \equiv 76. Therefore

2100=2802207676=577676(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.5Casting out nines and the test for 11

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

Nkdk(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=393+9=123(mod9)9+8+7+6+5+4 = 39 \equiv 3+9 = 12 \equiv 3 \pmod 9, so N3(mod9)N \equiv 3 \pmod 9.

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

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

For 987654987654, attaching alternating signs starting from the units digit gives 45+67+89=38(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.6Cancellation law

Let nNn \in \mathbb{N}, cZc \in \mathbb{Z}, and d=gcd(c,n)d = \gcd(c,n). Then

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

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

Proof(Proposition 3.6)

Write c=dcc = dc' and n=dnn = 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 cacb(modn)ca \equiv cb \pmod n says nc(ab)n \mid c(a-b), that is, dndc(ab)dn' \mid dc'(a-b); dividing both sides by dd gives nc(ab)n' \mid c'(a-b). Since gcd(c,n)=1\gcd(c',n')=1, Lemma 2.3 yields n(ab)n' \mid (a-b), that is, ab(modn)a \equiv b \pmod{n'}.

()(\Leftarrow) If n(ab)n' \mid (a-b) then n=dndc(ab)=c(ab)n = dn' \mid dc'(a-b) = c(a-b), so cacb(modn)ca \equiv cb \pmod n.

Example 3.7An example where division breaks down

We have 23=62 \cdot 3 = 6 and 29=182 \cdot 9 = 18, and 186=1218 - 6 = 12, so 2329(mod12)2 \cdot 3 \equiv 2 \cdot 9 \pmod{12}. But 93=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 39(mod6)3 \equiv 9 \pmod 6 — and indeed 93=69 - 3 = 6 satisfies this.

Definition 4.1Ring of residue classes

Let nNn \in \mathbb{N}. We write

Z/nZ={[0],[1],,[n1]}\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]=[ab][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.3Group 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 ax1=nyax - 1 = -ny, that is, ax1(modn)ax \equiv 1 \pmod n, so [a][x]=[1][a][x] = [1].

()(\Rightarrow) If [a][b]=[1][a][b] = [1], then ab1=nkab - 1 = nk for some integer kk, so abnk=1ab - nk = 1. Now d=gcd(a,n)d = \gcd(a,n) divides both terms on the left, so d1d \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 pap \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 p2p \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.6Euler's totient function

For nNn \in \mathbb{N} set

φ(n):=#{aZ1an, 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.7The 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.8Chinese remainder theorem

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

Φ:Z/mnZZ/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 xr(modm)x \equiv r \pmod m and xs(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(aa)mn \mid (a-a'), hence in particular m(aa)m \mid (a-a') and n(aa)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 mam \mid a and nan \mid a. Writing a=mka = mk we get nmkn \mid mk, and since gcd(n,m)=1\gcd(n,m)=1, Lemma 2.3 gives nkn \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 mnm \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=p1e1pkekn = p_1^{e_1}\cdots p_k^{e_k} is the prime factorization of nn, then

φ(n)=ni=1k(11pi).\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)Φ(u1)=Φ(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,,pe1pp, 2p, \ldots, p^{e-1}\cdot p, of which there are pe1p^{e-1}. Hence

φ(pe)=pepe1=pe(11p).\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.1Fermat's little theorem

Let pp be prime and let aa be an integer with pap \nmid a. Then

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

Let S={1,2,,p1}S = \{1, 2, \ldots, p-1\} and define a map σ\sigma by sending xSx \in S to axmodpax \bmod p. We argue in three steps.

Step one (the values of σ\sigma lie in SS). From 1xp11 \le x \le p-1 and pap \nmid a, the second half of Lemma 2.3 gives paxp \nmid ax. Hence axmodp0ax \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 axay(modp)ax \equiv ay \pmod p. From pap \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 xy(modp)x \equiv y \pmod p. Since xx and yy both lie between 11 and p1p-1, we have xy<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,

xSσ(x)=xSx=(p1)!.\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 p1p-1 times,

xSσ(x)xS(ax)=ap1(p1)!(modp).\prod_{x \in S}\sigma(x) \equiv \prod_{x\in S} (ax) = a^{p-1}(p-1)! \pmod p .

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

Corollary 5.2

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

Proof(Corollary 5.2)

If pap \nmid a, multiply both sides of Theorem 5.1 by aa to get apa(modp)a^p \equiv a \pmod p. If pap \mid a, then a0(modp)a \equiv 0 \pmod p, so Proposition 3.3 gives ap0p=0a(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.3Euler's theorem

Let nNn \in \mathbb{N} and aZa \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]:UU\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 P1P^{-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 pap \nmid a. Then the inverse of [a][a] in Z/pZ\mathbb{Z}/p\mathbb{Z} is given by [ap2][a^{p-2}].

Proof(Proposition 5.5)

Since p2p \ge 2 we have p20p - 2 \ge 0, so ap2a^{p-2} is an integer. By Theorem 5.1,

[a][ap2]=[ap1]=[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.63 to the 1000th power modulo 7

Since 77 is prime and 737 \nmid 3, Theorem 5.1 gives 361(mod7)3^6 \equiv 1 \pmod 7. As 1000=6166+41000 = 6 \cdot 166 + 4,

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

Since 81=711+481 = 7\cdot 11 + 4, we get 310004(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 aemodna^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(loge)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.1Correctness of RSA

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

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

Then for every integer mm,

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

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

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

med=m(mp1)k(q1)m1k(q1)=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 medm(modq)m^{ed}\equiv m \pmod q.

Hence p(medm)p \mid (m^{ed}-m) and q(medm)q \mid (m^{ed}-m). Since pqp \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(medm)pq \mid (m^{ed}-m), that is, medm(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.

flowchart TD
A["choose primes p, q"] --> B["n = pq, phi = (p-1)(q-1)"]
B --> C["choose e with gcd(e, phi) = 1"]
C --> D["find d = e^-1 mod phi by extended Euclid"]
D --> E["public key (n, e)"]
D --> F["private key (n, d)"]
E --> G["encrypt c = m^e mod n"]
G --> H["decrypt m = c^d mod n"]
F --> H
Key generation, encryption, and decryption in RSA. Security rests on the difficulty of recovering p and q from n.

Example 6.2Carrying RSA through with a small key

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

Encrypt the plaintext m=5m = 5:

52=25,54252=625=4143+5353(mod143),5^2 = 25,\quad 5^4 \equiv 25^2 = 625 = 4\cdot 143 + 53 \equiv 53 \pmod{143},c=57=5452553255(mod143).c = 5^7 = 5^4\cdot 5^2 \cdot 5 \equiv 53 \cdot 25 \cdot 5 \pmod{143}.

Since 5325=1325=9143+383853\cdot 25 = 1325 = 9\cdot 143 + 38 \equiv 38 and 385=190=143+474738 \cdot 5 = 190 = 143 + 47 \equiv 47, we get c=47c = 47.

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

Modulo 1111: 47=411+3347 = 4\cdot 11 + 3 \equiv 3, and Theorem 5.1 gives 3101(mod11)3^{10}\equiv 1 \pmod{11}; since 103=1010+3103 = 10\cdot 10 + 3, we get 4710333=275(mod11)47^{103} \equiv 3^{3} = 27 \equiv 5 \pmod{11}.

Modulo 1313: 47=313+8847 = 3\cdot 13 + 8 \equiv 8 and 8121(mod13)8^{12}\equiv 1 \pmod{13}; since 103=128+7103 = 12\cdot 8 + 7, we get 4710387(mod13)47^{103}\equiv 8^{7} \pmod{13}. Here 82=64=413+1218^2 = 64 = 4\cdot 13 + 12 \equiv -1, so 87=(82)38(1)38=85(mod13)8^7 = (8^2)^3 \cdot 8 \equiv (-1)^3\cdot 8 = -8 \equiv 5 \pmod{13}.

The unique solution modulo 143143 of x5(mod11)x \equiv 5 \pmod{11} and x5(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 an1≢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 an11(modn)a^{n-1}\equiv 1 \pmod n holds does not make nn prime.

Definition 6.3Carmichael number

A composite number nn satisfying an11(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.4561 is a Carmichael number

We have 561=31117561 = 3\cdot 11\cdot 17, which is composite. Also 560560 is divisible by each of 2,10,162, 10, 16 (indeed 560=2280=1056=1635560 = 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

a21(mod3),a101(mod11),a161(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)2801(mod3)a^{560} = (a^{2})^{280} \equiv 1 \pmod 3 and similarly for the others, so a5601a^{560}\equiv 1 modulo all three primes. Using Theorem 4.8 twice yields 561(a5601)561 \mid (a^{560}-1), that is, a5601(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 n1=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)=14=4\varphi(10)=\varphi(2)\varphi(5)=1\cdot 4=4 (Corollary 4.9), so Theorem 5.3 gives 741(mod10)7^{4}\equiv 1 \pmod{10}. Since 2026=4506+22026 = 4\cdot 506 + 2,

72026=(74)506721506499(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 (p1)!1(modp)(p-1)! \equiv -1 \pmod p for every prime pp (Wilson’s theorem).

Solution

For p=2p = 2 we have 1!=11(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],,[p1][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(x1)(x+1)p \mid (x-1)(x+1), and Lemma 2.3 gives p(x1)p \mid (x-1) or p(x+1)p \mid (x+1), that is, x1x \equiv 1 or x1(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 [p1][p-1].

The remaining p3p-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

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

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

Exercise 7.3Standard

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

Solution

Base 22: from 210=1024=9311+12^{10}=1024 = 93\cdot 11 + 1 we get 2101(mod11)2^{10}\equiv 1 \pmod{11}, and since 340=1034340 = 10\cdot 34, 23401(mod11)2^{340}\equiv 1 \pmod{11}. Also 25=32=31+11(mod31)2^{5}=32 = 31+1 \equiv 1 \pmod{31} and 340=568340 = 5\cdot 68, so 23401(mod31)2^{340}\equiv 1 \pmod{31}. Since gcd(11,31)=1\gcd(11,31)=1, Theorem 4.8 gives 341(23401)341 \mid (2^{340}-1), that is, 23401(mod341)2^{340}\equiv 1\pmod{341}.

Base 33: modulo 1111 we have 35=243=2211+113^{5}=243 = 22\cdot 11 + 1 \equiv 1 and 340=568340 = 5\cdot 68, so 33401(mod11)3^{340}\equiv 1 \pmod{11}. Modulo 3131, Theorem 5.1 gives 33013^{30}\equiv 1, and since 340=3011+10340 = 30\cdot 11 + 10 we get 3340310(mod31)3^{340}\equiv 3^{10} \pmod{31}. Here 33=273^{3}=27 and 35=243=731+2653^{5}=243 = 7\cdot 31 + 26 \equiv -5, so 310(5)2=25(mod31)3^{10} \equiv (-5)^2 = 25 \pmod{31}.

Since 334025≢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 x1(mod11)x\equiv 1 \pmod{11} and x25(mod31)x \equiv 25 \pmod{31}. Putting x=25+31kx = 25 + 31k and using 25325 \equiv 3, 319(mod11)31\equiv 9 \pmod{11}, we need 3+9k13 + 9k \equiv 1, that is, 9k29(mod11)9k \equiv -2 \equiv 9 \pmod{11}; since gcd(9,11)=1\gcd(9,11)=1, Proposition 3.6 gives k1(mod11)k \equiv 1 \pmod{11}. Taking k=1k=1 gives x=56x = 56, so 334056(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 (p1)(n1)(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 p2np^{2}\mid n. From the prime factorization of nn we may write n=pemn = p^{e}m with e2e \ge 2 and pmp \nmid m. Since gcd(p2,m)=1\gcd(p^{2}, m)=1, Theorem 4.8 supplies an integer aa with

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

This aa is not divisible by pp (because a1(modp)a \equiv 1 \pmod p) and is divisible by no prime factor of mm (because a1(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)k1+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 an11(modp2)a^{n-1}\equiv 1 \pmod{p^{2}} is equivalent to p2(n1)pp^{2} \mid (n-1)p, that is, to p(n1)p \mid (n-1). But pnp \mid n, so p(n1)p \nmid (n-1) — a contradiction. Therefore nn is squarefree.

The divisibility (p1)(n1)(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 p1p-1. Take a generator gg, and use Theorem 4.8 to obtain aa with ag(modp)a \equiv g \pmod p and a1(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, an11(modn)a^{n-1}\equiv 1 \pmod n, and in particular an11(modp)a^{n-1}\equiv 1 \pmod p. That is, gn1=1g^{n-1} = 1 holds in (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^{\times}. The order of gg is p1p-1, so the only kk with gk=1g^{k}=1 are the multiples of p1p-1. Hence (p1)(n1)(p-1)\mid(n-1).

(For 561561, that 31=23-1=2, 111=1011-1=10, and 171=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 a1a \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, kpk \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 apaa^{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(apa),p \mid (a^{p}-a) ,

which is nothing other than Corollary 5.2. When pap \nmid a we have gcd(a,p)=1\gcd(a,p)=1, so Proposition 3.6 lets us cancel aa and obtain ap11(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]).

Report an error in this article ・Operated by: Mugen Giken LLCPricingTermsLegal notice

© 2026 夢現技研合同会社 ・Feeding the text to an LLM is welcome. Code samples are MIT licensed.