MATH2400 2,012 words·11 min read

Fermats Little Theorem and Eulers Theorem

Powers in Modular Arithmetic#

In the last topic we dealt with linear congruences like ax≡c(modn)ax \equiv c \pmod n. The natural next step is congruences involving higher powers of xx, such as x2≡c(modn)x^2 \equiv c \pmod n; but before we can say anything about those, we need to understand how powers behave in Zn\mathbb{Z}_n in the first place.

Consider the powers of every element of Z7\mathbb{Z}_7:

xkx^k k=1k=1 22 33 44 55 66 77 …\dots
0k0^k 00 00 00 00 00 00 00 …\dots
1k1^k 11 11 11 11 11 11 11 …\dots
2k2^k 22 44 11 22 44 11 22 …\dots
3k3^k 33 22 66 44 55 11 33 …\dots
4k4^k 44 22 11 44 22 11 44 …\dots
5k5^k 55 44 66 22 33 11 55 …\dots
6k6^k 66 11 66 11 66 11 66 …\dots

Notice how much structure is hiding in here:

  • In every row except 0k0^k, a 11 eventually appears.
  • Column 66 is (almost) all 11's, and column 77 is identical to column 11; raising to the power 77 does nothing in Z7\mathbb{Z}_7.
  • The first 11 in each row appears in column 1,2,31, 2, 3 or 66; always a divisor of 66.
  • The cycle of entries in row xkx^k is the reverse of the cycle in row (x−1)k(x^{-1})^k.
  • Going down the rows, the entries of column 22 cycle with length 33, and the entries of column 33 cycle with length 22.

Running the same experiment in Z11\mathbb{Z}_{11} tells the same story: column 1010 is (almost) all 11's, column 1111 repeats column 11, and the first 11 of each row lands in column 1,2,51, 2, 5 or 1010 — again, always a divisor of 1010.

So in Zp\mathbb{Z}_p it looks like ap−1=1a^{p-1} = 1 for every a≠0a \neq 0, and ap=aa^p = a for absolutely every aa. This is not a coincidence.

Fermat's Little Theorem#

Note

Fermat's Little Theorem
For any prime pp and any integer aa, we have

ap≡a(modp).a^p \equiv a \pmod{p}.

Similarly, for any prime pp and any integer aa such that p∤ap \nmid a, we have

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

Basically, modulo a prime, raising to the power pp is invisible, and every residue that is not a multiple of pp resets to 11 after exactly p−1p-1 steps; this is precisely the "column 66 is all 11's, column 77 copies column 11" pattern from the tables. For p∤ap \nmid a the two forms are equivalent (multiply the second by aa to get the first), but they are not interchangeable in general. The second form requires p∤ap \nmid a; if p∣ap \mid a then a≡0(modp)a \equiv 0 \pmod p and no power of it will ever be 11.

Proof.
If p∣ap \mid a, then a≡0(modp)a \equiv 0 \pmod p, so ap≡0≡a(modp)a^p \equiv 0 \equiv a \pmod p and the first form holds trivially.
So suppose that p∤ap \nmid a, and consider the p−1p-1 numbers

a,  2a,  3a,  …,  (p−1)a(modp).a, \; 2a, \; 3a, \; \dots, \; (p-1)a \pmod p.

None of these is 00 modulo pp; if p∣iap \mid ia then, since pp is prime, p∣ip \mid i or p∣ap \mid a, and both are impossible for 1≤i≤p−11 \leq i \leq p-1. They are also pairwise distinct modulo pp; if ia≡ja(modp)ia \equiv ja \pmod p then p∣(i−j)ap \mid (i-j)a, and since p∤ap \nmid a we must have p∣i−jp \mid i-j, which forces i=ji = j as ∣i−j∣≤p−2<p|i-j| \leq p-2 < p.

So the list above is just 1,2,…,p−11, 2, \dots, p-1 rearranged. Multiplying everything together,

a⋅2a⋅3a⋯(p−1)a≡1⋅2⋅3⋯(p−1)(modp),ap−1 (p−1)!≡(p−1)!(modp).\begin{align*} a \cdot 2a \cdot 3a \cdots (p-1)a &\equiv 1 \cdot 2 \cdot 3 \cdots (p-1) \pmod p, \\ a^{p-1} \, (p-1)! &\equiv (p-1)! \pmod p. \end{align*}

Since pp is prime, it divides none of 1,2,…,p−11, 2, \dots, p-1, so gcd⁡((p−1)!,p)=1\gcd((p-1)!, p) = 1 and (p−1)!(p-1)! is a unit modulo pp; cancelling it from both sides gives

ap−1≡1(modp).■a^{p-1} \equiv 1 \pmod p. \qquad \blacksquare

To see the rearrangement step in action, take p=7p = 7 and a=3a = 3;

{3,  6,  9,  12,  15,  18}≡{3,  6,  2,  5,  1,  4}(mod7),\{3, \; 6, \; 9, \; 12, \; 15, \; 18\} \equiv \{3, \; 6, \; 2, \; 5, \; 1, \; 4\} \pmod 7,

which really is {1,2,3,4,5,6}\{1,2,3,4,5,6\} shuffled.

Example. Find 5097 mod 9750^{97} \bmod 97.
Here 9797 is prime, so the first form of Fermat's Little Theorem applies to every integer with no coprimality check needed;

5097≡50(mod97).\begin{align*} 50^{97} &\equiv 50 \pmod{97}. \end{align*}

Therefore, 5097 mod 97=5050^{97} \bmod 97 = 50; the exponent matching the modulus means we get the answer for free.

Example. Find 99100 mod 10199^{100} \bmod 101, and hence find 99909 mod 10199^{909} \bmod 101.
Since 101101 is prime and 101∤99101 \nmid 99, the second form gives

99100≡1(mod101),99^{100} \equiv 1 \pmod{101},

so 99100 mod 101=199^{100} \bmod 101 = 1. For the second power, we peel off as many blocks of 100100 from the exponent as possible; each block collapses to 11. Writing 909=9⋅100+9909 = 9 \cdot 100 + 9 and noticing that 99≡−2(mod101)99 \equiv -2 \pmod{101},

99909=(99100)9⋅999≡19⋅999(mod101)≡(−2)9(mod101)≡−512(mod101)≡−512+6⋅101(mod101)≡94(mod101).\begin{align*} 99^{909} &= \left(99^{100}\right)^{9} \cdot 99^{9} \\ &\equiv 1^{9} \cdot 99^{9} \pmod{101} \\ &\equiv (-2)^{9} \pmod{101} \\ &\equiv -512 \pmod{101} \\ &\equiv -512 + 6 \cdot 101 \pmod{101} \\ &\equiv 94 \pmod{101}. \end{align*}

Therefore, 99909 mod 101=9499^{909} \bmod 101 = 94. Notice how switching 9999 for −2-2 turned an impossible-looking power into something you can do by hand.

Reducing Exponents Modulo a Prime#

The "peel off blocks of p−1p-1" trick from the last example is worth stating as a theorem in its own right.

Note

Theorem
For any prime pp and integer aa such that p∤ap \nmid a, we have for all integers kk that

ak≡a(k mod p−1)(modp).a^k \equiv a^{(k \bmod p-1)} \pmod{p}.

Proof. By the division theorem, write k=q(p−1)+rk = q(p-1) + r where r=k mod (p−1)r = k \bmod (p-1), so 0≤r<p−10 \leq r < p - 1. Then

ak=aq(p−1)+r=(ap−1)q⋅ar≡1q⋅ar(modp), (by Fermat’s Little Theorem, as p∤a)≡ar(modp).■\begin{align*} a^{k} &= a^{q(p-1) + r} \\ &= \left(a^{p-1}\right)^{q} \cdot a^{r} \\ &\equiv 1^{q} \cdot a^{r} \pmod p, \text{ (by Fermat's Little Theorem, as } p \nmid a\text{)} \\ &\equiv a^{r} \pmod p. \qquad \blacksquare \end{align*}

Basically, when working modulo a prime pp, the bases live in Zp\mathbb{Z}_p but the exponents live in Zp−1\mathbb{Z}_{p-1}. It is important to remember that exponents get reduced modulo p−1p-1, not modulo pp; reducing an exponent mod pp is the classic exam mistake.

p∤a  ⟹  ak≡ak mod (p−1)(modp).\boxed{p \nmid a \implies a^k \equiv a^{k \bmod (p-1)} \pmod p.}

Example. Find 31000 mod 73^{1000} \bmod 7.
Here 77 is prime and 7∤37 \nmid 3, so exponents can be reduced modulo 7−1=67 - 1 = 6;

1000=6⋅166+4,31000≡34(mod7)≡81(mod7)≡81−77(mod7)≡4(mod7).\begin{align*} 1000 &= 6 \cdot 166 + 4, \\ 3^{1000} &\equiv 3^{4} \pmod 7 \\ &\equiv 81 \pmod 7 \\ &\equiv 81 - 77 \pmod 7 \\ &\equiv 4 \pmod 7. \end{align*}

Therefore, 31000 mod 7=43^{1000} \bmod 7 = 4.

Powers in Non-Prime Moduli#

Fermat's Little Theorem is strictly about prime moduli. To see what breaks otherwise, here is the power table for Z9\mathbb{Z}_9:

xkx^k k=1k=1 22 33 44 55 66 77 …\dots
0k0^k 00 00 00 00 00 00 00 …\dots
1k1^k 11 11 11 11 11 11 11 …\dots
2k2^k 22 44 88 77 55 11 22 …\dots
3k3^k 33 00 00 00 00 00 00 …\dots
4k4^k 44 77 11 44 77 11 44 …\dots
5k5^k 55 77 88 44 22 11 55 …\dots
6k6^k 66 00 00 00 00 00 00 …\dots
7k7^k 77 44 11 77 44 11 77 …\dots
8k8^k 88 11 88 11 88 11 88 …\dots

We can observe some familiar patterns, and one new pathology:

  • A 11 eventually appears in every row except 0k0^k, 3k3^k and 6k6^k.
  • Column 66 is mostly 11's, and column 77 is similar to column 11.
  • The first 11 in each row appears in column 1,2,31, 2, 3 or 66.
  • The rows for 33 and 66 collapse to 00 and never recover.

The rows that die are exactly the elements sharing a factor with 99; indeed 32=9≡03^2 = 9 \equiv 0, and once you hit 00 you stay there. The rows that do return to 11 are exactly the units of Z9\mathbb{Z}_9, and there are 66 of them — which explains why column 66 plays the role that column p−1p - 1 played in the prime tables. What we need is a function that counts these "good" elements for an arbitrary modulus.

Euler's Totient Function#

Note

Definition
The (Euler) totient function (or Euler's phi function) is the function ϕ\phi that for any positive integer nn outputs the number of positive integers less than or equal to nn that are coprime with nn. That is,

ϕ(n)=∣{a∈Z:1≤a≤n and gcd⁡(a,n)=1}∣.\phi(n) = \left| \{a \in \mathbb{Z} : 1 \leq a \leq n \text{ and } \gcd(a,n) = 1\} \right|.

Recall from Modular Rings and Units that a∈Zn∗  ⟺  gcd⁡(a,n)=1a \in \mathbb{Z}_n^* \iff \gcd(a,n) = 1; so ϕ(n)\phi(n) is exactly the number of units in Zn\mathbb{Z}_n,

ϕ(n)=∣Zn∗∣.\boxed{\phi(n) = |\mathbb{Z}_n^*|.}

Basically, ϕ(n)\phi(n) counts how many residues mod nn you are allowed to divide by; equivalently, how many rows of the power table will eventually return to 11.

Example. Find the following values.

  • ϕ(7)=6\phi(7) = 6; since 77 is prime, all of 1,2,3,4,5,61, 2, 3, 4, 5, 6 are coprime with it.
  • ϕ(9)=6\phi(9) = 6; we only delete the multiples of 33, leaving {1,2,4,5,7,8}\{1,2,4,5,7,8\} — matching the six "surviving" rows in the Z9\mathbb{Z}_9 table above.
  • ϕ(12)=4\phi(12) = 4; the survivors are {1,5,7,11}\{1, 5, 7, 11\}.
  • ϕ(15)=8\phi(15) = 8; the survivors are {1,2,4,7,8,11,13,14}\{1, 2, 4, 7, 8, 11, 13, 14\}.
  • For any prime number pp, we have ϕ(p)=p−1\phi(p) = p - 1; every one of 1,2,…,p−11, 2, \dots, p-1 is coprime with pp.

Notice that the p−1p - 1 in Fermat's Little Theorem was really ϕ(p)\phi(p) in disguise; this is the observation that Euler generalised.

Totients of Prime Powers#

Computing ϕ(n)\phi(n) straight from the definition means checking a gcd for every single a≤na \leq n, which is hopeless for large nn. The following two results reduce the whole computation to knowing the prime factorisation of nn.

Note

Theorem
For any prime pp and k∈Z+k \in \mathbb{Z}^+,

ϕ(pk)=pk−pk−1=pk−1(p−1).\phi(p^k) = p^k - p^{k-1} = p^{k-1}(p-1).

Proof. The only prime factor of pkp^k is pp, so gcd⁡(a,pk)>1\gcd(a, p^k) > 1 if and only if p∣ap \mid a. Hence we take all pkp^k integers in 1,2,…,pk1, 2, \dots, p^k and delete the multiples of pp, which are

p,  2p,  3p,  …,  pk−1⋅p;p, \; 2p, \; 3p, \; \dots, \; p^{k-1} \cdot p;

there are exactly pk−1p^{k-1} of them. Everything remaining is coprime with pkp^k, so ϕ(pk)=pk−pk−1\phi(p^k) = p^k - p^{k-1}. ■\blacksquare

Basically, exactly 11 in every pp consecutive integers is a multiple of pp, so a prime power keeps a (1−1p)\left(1 - \frac{1}{p}\right) share of its residues as units.

Example. Find ϕ(8)\phi(8) and ϕ(125)\phi(125).

ϕ(8)=ϕ(23)=23−22=8−4=4,ϕ(125)=ϕ(53)=53−52=125−25=100.\begin{align*} \phi(8) = \phi(2^3) &= 2^3 - 2^2 \\ &= 8 - 4 \\ &= 4, \\ \phi(125) = \phi(5^3) &= 5^3 - 5^2 \\ &= 125 - 25 \\ &= 100. \end{align*}

As a sanity check for the first one, the odd residues {1,3,5,7}\{1, 3, 5, 7\} are precisely the units of Z8\mathbb{Z}_8; there are indeed 44 of them. Therefore, ϕ(8)=4\phi(8) = 4 and ϕ(125)=100\phi(125) = 100.

The Totient is Multiplicative#

Note

Theorem
For any m,n∈Z+m, n \in \mathbb{Z}^+ with gcd⁡(m,n)=1\gcd(m,n) = 1, we have

ϕ(mn)=ϕ(m)ϕ(n).\phi(mn) = \phi(m)\phi(n).

Proof. First, notice that gcd⁡(a,mn)=1\gcd(a, mn) = 1 if and only if gcd⁡(a,m)=1\gcd(a,m) = 1 and gcd⁡(a,n)=1\gcd(a,n) = 1; any prime dividing both aa and mnmn must divide mm or nn, and conversely any common prime factor of aa and mm (or nn) also divides mnmn.

Now write the integers 1,2,…,mn1, 2, \dots, mn in a grid with mm columns, so that column cc contains

c,  m+c,  2m+c,  …,  (n−1)m+c.c, \; m + c, \; 2m + c, \; \dots, \; (n-1)m + c.

Every entry of column cc is congruent to cc modulo mm; so the columns containing numbers coprime with mm are exactly the columns with gcd⁡(c,m)=1\gcd(c, m) = 1, and there are ϕ(m)\phi(m) of these.

Fix one such column and reduce its nn entries modulo nn. They are pairwise distinct; if im+c≡jm+c(modn)im + c \equiv jm + c \pmod n then n∣(i−j)mn \mid (i - j)m, and since gcd⁡(m,n)=1\gcd(m,n) = 1 we get n∣i−jn \mid i - j, forcing i=ji = j as ∣i−j∣<n|i-j| < n. So the nn entries of the column hit every residue modulo nn exactly once, and hence exactly ϕ(n)\phi(n) of them are coprime with nn.

Counting: ϕ(m)\phi(m) good columns, each contributing ϕ(n)\phi(n) entries coprime with both mm and nn; i.e. coprime with mnmn. Therefore ϕ(mn)=ϕ(m)ϕ(n)\phi(mn) = \phi(m)\phi(n). ■\blacksquare

To see this concretely with m=3m = 3 and n=4n = 4, lay out 11 to 1212:

c=1c=1 c=2c=2 c=3c=3
11 22 33
44 55 66
77 88 99
1010 1111 1212

Column 33 is dead (everything shares the factor 33), while columns 11 and 22 each contain every residue mod 44 once, hence ϕ(4)=2\phi(4) = 2 units each. That gives ϕ(3)ϕ(4)=2⋅2=4\phi(3)\phi(4) = 2 \cdot 2 = 4 units in total, and indeed Z12∗={1,5,7,11}\mathbb{Z}_{12}^* = \{1, 5, 7, 11\}.

It is important to note that ϕ\phi is only multiplicative when gcd⁡(m,n)=1\gcd(m,n) = 1; for instance ϕ(4)=2\phi(4) = 2 but ϕ(2)ϕ(2)=1⋅1=1\phi(2)\phi(2) = 1 \cdot 1 = 1. Always split nn into coprime pieces (i.e. into distinct prime powers), never arbitrary factors.

Example. Find ϕ(360)\phi(360).
Factorising and splitting into pairwise coprime prime powers,

360=23⋅32⋅5,ϕ(360)=ϕ(23) ϕ(32) ϕ(5)=(23−22)(32−3)(5−1)=4⋅6⋅4=96.\begin{align*} 360 &= 2^3 \cdot 3^2 \cdot 5, \\ \phi(360) &= \phi(2^3) \, \phi(3^2) \, \phi(5) \\ &= (2^3 - 2^2)(3^2 - 3)(5 - 1) \\ &= 4 \cdot 6 \cdot 4 \\ &= 96. \end{align*}

Therefore, ϕ(360)=96\phi(360) = 96; there are 9696 units in Z360\mathbb{Z}_{360}.

Combining the two theorems gives a one-line formula:

Note

Corollary
For any n∈Z+n \in \mathbb{Z}^+, given that the distinct prime factors of nn are p1,p2,…,pkp_1, p_2, \dots, p_k, we have

ϕ(n)=n∏i=1k(1−1pi)=n(1−1p1)(1−1p2)⋯(1−1pk).\phi(n) = n \prod_{i=1}^{k} \left(1 - \frac{1}{p_i}\right) = n \left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right) \cdots \left(1 - \frac{1}{p_k}\right).

Basically, each distinct prime factor independently deletes its own 1p\frac{1}{p} share of the residues. Notice that the exponents in the factorisation never appear in the product; only which primes divide nn matters, with the exponents hiding inside the leading factor of nn.

Example. Find ϕ(1000)\phi(1000).

1000=23⋅53,ϕ(1000)=1000(1−12)(1−15)=1000⋅12⋅45=400.\begin{align*} 1000 &= 2^3 \cdot 5^3, \\ \phi(1000) &= 1000 \left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{5}\right) \\ &= 1000 \cdot \frac{1}{2} \cdot \frac{4}{5} \\ &= 400. \end{align*}

Therefore, ϕ(1000)=400\phi(1000) = 400.

Euler's Theorem#

We can now generalise Fermat's Little Theorem to any modulus, with ϕ(n)\phi(n) stepping into the role of p−1p - 1.

Note

Euler's Theorem
For any a∈Za \in \mathbb{Z} and any n∈Z+n \in \mathbb{Z}^+ such that gcd⁡(a,n)=1\gcd(a,n) = 1, we have

aϕ(n)≡1(modn).a^{\phi(n)} \equiv 1 \pmod{n}.

Equivalently, for any n∈Z+n \in \mathbb{Z}^+, we have that for all a∈Zn⋆a \in \mathbb{Z}_n^\star,

aϕ(n)=1 in Zn.a^{\phi(n)} = 1 \text{ in } \mathbb{Z}_n.

Setting n=pn = p prime recovers Fermat's Little Theorem exactly, since ϕ(p)=p−1\phi(p) = p - 1 and gcd⁡(a,p)=1\gcd(a, p) = 1 is the same condition as p∤ap \nmid a. Basically, the units of Zn\mathbb{Z}_n all return to 11 after ϕ(n)\phi(n) steps — which is why column 66 worked in the Z9\mathbb{Z}_9 table (ϕ(9)=6\phi(9) = 6) even though 99 is not prime. Unlike Fermat's Little Theorem, there is no "first form" here: if gcd⁡(a,n)≠1\gcd(a,n) \neq 1 then Euler's Theorem says nothing at all about aa.

The proof is the same permutation trick as before, but run over the units instead of over all nonzero residues.

Proof. Let r1,r2,…,rϕ(n)r_1, r_2, \dots, r_{\phi(n)} be the elements of Zn∗\mathbb{Z}_n^*; by definition of ϕ\phi there are exactly ϕ(n)\phi(n) of them. Since gcd⁡(a,n)=1\gcd(a,n) = 1, aa is itself a unit, and products of units are units, so each ari∈Zn∗ar_i \in \mathbb{Z}_n^*. Moreover the ariar_i are pairwise distinct in Zn\mathbb{Z}_n; if ari≡arj(modn)ar_i \equiv ar_j \pmod n, multiplying both sides by a−1a^{-1} (which exists as aa is a unit) gives ri≡rjr_i \equiv r_j, so i=ji = j. Hence ar1,ar2,…,arϕ(n)ar_1, ar_2, \dots, ar_{\phi(n)} is just r1,r2,…,rϕ(n)r_1, r_2, \dots, r_{\phi(n)} rearranged. Multiplying everything together,

(ar1)(ar2)⋯(arϕ(n))≡r1r2⋯rϕ(n)(modn),aϕ(n) R≡R(modn), (where R=r1r2⋯rϕ(n)).\begin{align*} (ar_1)(ar_2) \cdots (ar_{\phi(n)}) &\equiv r_1 r_2 \cdots r_{\phi(n)} \pmod n, \\ a^{\phi(n)} \, R &\equiv R \pmod n, \text{ (where } R = r_1 r_2 \cdots r_{\phi(n)}\text{)}. \end{align*}

Since RR is a product of units, it is itself a unit; multiplying both sides by R−1R^{-1} gives

aϕ(n)≡1(modn).■a^{\phi(n)} \equiv 1 \pmod n. \qquad \blacksquare

For a concrete instance of the shuffle, take n=15n = 15 and a=7a = 7; the units of Z15\mathbb{Z}_{15} are {1,2,4,7,8,11,13,14}\{1, 2, 4, 7, 8, 11, 13, 14\}, and multiplying each by 77 gives

{7,  14,  13,  4,  11,  2,  1,  8}(mod15),\{7, \; 14, \; 13, \; 4, \; 11, \; 2, \; 1, \; 8\} \pmod{15},

the same set shuffled.

Example. Find 789 mod 157^{89} \bmod 15.
First, gcd⁡(7,15)=1\gcd(7, 15) = 1, so Euler's Theorem applies, and

ϕ(15)=ϕ(3)ϕ(5)=2⋅4=8,\phi(15) = \phi(3)\phi(5) = 2 \cdot 4 = 8,

so 78≡1(mod15)7^8 \equiv 1 \pmod{15}. Reducing the exponent,

89=8⋅11+1,789=(78)11⋅71≡111⋅7(mod15)≡7(mod15).\begin{align*} 89 &= 8 \cdot 11 + 1, \\ 7^{89} &= \left(7^{8}\right)^{11} \cdot 7^{1} \\ &\equiv 1^{11} \cdot 7 \pmod{15} \\ &\equiv 7 \pmod{15}. \end{align*}

Therefore, 789 mod 15=77^{89} \bmod 15 = 7.

Example. Find 544 mod 185^{44} \bmod 18 and 577 mod 185^{77} \bmod 18.
Here gcd⁡(5,18)=1\gcd(5, 18) = 1 and

ϕ(18)=ϕ(2)ϕ(9)=1⋅6=6,\phi(18) = \phi(2)\phi(9) = 1 \cdot 6 = 6,

so 56≡1(mod18)5^6 \equiv 1 \pmod{18} and exponents can be reduced modulo 66. For the first power, 44=6⋅7+244 = 6 \cdot 7 + 2, so

544≡52(mod18)≡25(mod18)≡7(mod18).\begin{align*} 5^{44} &\equiv 5^{2} \pmod{18} \\ &\equiv 25 \pmod{18} \\ &\equiv 7 \pmod{18}. \end{align*}

For the second, 77=6⋅12+577 = 6 \cdot 12 + 5, so 577≡55(mod18)5^{77} \equiv 5^5 \pmod{18}. Rather than grinding out 55=31255^5 = 3125, notice that 53=125=126−1≡−1(mod18)5^3 = 125 = 126 - 1 \equiv -1 \pmod{18};

577≡55(mod18)≡53⋅52(mod18)≡(−1)⋅7(mod18)≡−7(mod18)≡11(mod18).\begin{align*} 5^{77} &\equiv 5^{5} \pmod{18} \\ &\equiv 5^{3} \cdot 5^{2} \pmod{18} \\ &\equiv (-1) \cdot 7 \pmod{18} \\ &\equiv -7 \pmod{18} \\ &\equiv 11 \pmod{18}. \end{align*}

Therefore, 544 mod 18=75^{44} \bmod 18 = 7 and 577 mod 18=115^{77} \bmod 18 = 11. Spotting a power congruent to −1-1 is one of the best shortcuts available; it halves the effective cycle length.

Example. Find 2100 mod 122^{100} \bmod 12.
The tempting move is: ϕ(12)=4\phi(12) = 4 and 100≡0(mod4)100 \equiv 0 \pmod 4, "so" 2100≡20=1(mod12)2^{100} \equiv 2^0 = 1 \pmod{12}. This is wrong; gcd⁡(2,12)=2≠1\gcd(2, 12) = 2 \neq 1, so Euler's Theorem does not apply and you are not allowed to reduce the exponent. Indeed every positive power of 22 is even, so none of them can possibly be congruent to 11 modulo 1212.

The correct approach is to split 1212 into coprime pieces, 12=4⋅312 = 4 \cdot 3, and study each separately;

2100≡0(mod4), (since 4=22∣2100),2100≡(−1)100(mod3)≡1(mod3).\begin{align*} 2^{100} &\equiv 0 \pmod 4, \text{ (since } 4 = 2^2 \mid 2^{100}\text{)}, \\ 2^{100} &\equiv (-1)^{100} \pmod 3 \\ &\equiv 1 \pmod 3. \end{align*}

Now we need the unique xx with 0≤x<120 \leq x < 12 satisfying x≡0(mod4)x \equiv 0 \pmod 4 and x≡1(mod3)x \equiv 1 \pmod 3; checking the candidates 0,4,80, 4, 8, only x=4x = 4 works. Therefore, 2100 mod 12=42^{100} \bmod 12 = 4. Notice that Euler was still useful on the coprime piece (as gcd⁡(2,3)=1\gcd(2,3)=1); the trick is to quarantine the shared factor first.

Finding Inverses with Euler's Theorem#

Euler's Theorem also hands us a formula for inverses. Since

a⋅aϕ(n)−1=aϕ(n)≡1(modn),a \cdot a^{\phi(n) - 1} = a^{\phi(n)} \equiv 1 \pmod n,

we immediately get

gcd⁡(a,n)=1  ⟹  a−1≡aϕ(n)−1(modn).\boxed{\gcd(a,n) = 1 \implies a^{-1} \equiv a^{\phi(n)-1} \pmod n.}

This is an alternative to the extended Euclidean algorithm from Bezouts Identity and the Extended Euclidean Algorithm; it is usually slower by hand, but it is a one-line formula, which makes it useful in proofs (and in RSA-style computations later).

Example. Use Euler's Theorem to find 7−17^{-1} in Z30\mathbb{Z}_{30}.
We have gcd⁡(7,30)=1\gcd(7, 30) = 1 and

ϕ(30)=ϕ(2)ϕ(3)ϕ(5)=1⋅2⋅4=8,\phi(30) = \phi(2)\phi(3)\phi(5) = 1 \cdot 2 \cdot 4 = 8,

so 7−1≡77(mod30)7^{-1} \equiv 7^{7} \pmod{30}. Computing by squaring,

72=49≡19(mod30),74≡192(mod30)≡361(mod30)≡1(mod30),77=74⋅72⋅7≡1⋅19⋅7(mod30)≡133(mod30)≡13(mod30).\begin{align*} 7^{2} &= 49 \equiv 19 \pmod{30}, \\ 7^{4} &\equiv 19^{2} \pmod{30} \\ &\equiv 361 \pmod{30} \\ &\equiv 1 \pmod{30}, \\ 7^{7} &= 7^{4} \cdot 7^{2} \cdot 7 \\ &\equiv 1 \cdot 19 \cdot 7 \pmod{30} \\ &\equiv 133 \pmod{30} \\ &\equiv 13 \pmod{30}. \end{align*}

Check: 7⋅13=91=3⋅30+1≡1(mod30)7 \cdot 13 = 91 = 3 \cdot 30 + 1 \equiv 1 \pmod{30}. Therefore, 7−1=137^{-1} = 13 in Z30\mathbb{Z}_{30}.

Reducing Exponents in General#

Just as with primes, the real day-to-day use of Euler's Theorem is collapsing huge exponents.

Note

Theorem
For any n∈Z+n \in \mathbb{Z}^+ and a∈Za \in \mathbb{Z} such that gcd⁡(a,n)=1\gcd(a,n) = 1, we have for all k∈Zk \in \mathbb{Z} that

ak≡a(k mod ϕ(n))(modn).a^k \equiv a^{(k \bmod \phi(n))} \pmod{n}.

Proof. By the division theorem, write k=q ϕ(n)+rk = q\,\phi(n) + r where r=k mod ϕ(n)r = k \bmod \phi(n), so 0≤r<ϕ(n)0 \leq r < \phi(n). Then

ak=aqϕ(n)+r=(aϕ(n))q⋅ar≡1q⋅ar(modn), (by Euler’s Theorem, as gcd⁡(a,n)=1)≡ar(modn).■\begin{align*} a^{k} &= a^{q\phi(n) + r} \\ &= \left(a^{\phi(n)}\right)^{q} \cdot a^{r} \\ &\equiv 1^{q} \cdot a^{r} \pmod n, \text{ (by Euler's Theorem, as } \gcd(a,n) = 1\text{)} \\ &\equiv a^{r} \pmod n. \qquad \blacksquare \end{align*}

So when gcd⁡(a,n)=1\gcd(a,n) = 1, bases live in Zn\mathbb{Z}_n but exponents live in Zϕ(n)\mathbb{Z}_{\phi(n)};

gcd⁡(a,n)=1  ⟹  ak≡ak mod ϕ(n)(modn).\boxed{\gcd(a,n) = 1 \implies a^k \equiv a^{k \bmod \phi(n)} \pmod n.}

Exponents are reduced modulo ϕ(n)\phi(n), never modulo nn itself.

Example. Find 23100 mod 112^{3^{100}} \bmod 11.
This is a tower of powers, so we work from the outside in. The outer modulus 1111 is prime with gcd⁡(2,11)=1\gcd(2, 11) = 1, so the exponent 31003^{100} only matters modulo ϕ(11)=10\phi(11) = 10. That turns the problem into finding 3100 mod 103^{100} \bmod 10; and since gcd⁡(3,10)=1\gcd(3, 10) = 1 with ϕ(10)=ϕ(2)ϕ(5)=4\phi(10) = \phi(2)\phi(5) = 4, that exponent only matters modulo 44;

100≡0(mod4),3100≡30(mod10)≡1(mod10),23100≡21(mod11)≡2(mod11).\begin{align*} 100 &\equiv 0 \pmod 4, \\ 3^{100} &\equiv 3^{0} \pmod{10} \\ &\equiv 1 \pmod{10}, \\ 2^{3^{100}} &\equiv 2^{1} \pmod{11} \\ &\equiv 2 \pmod{11}. \end{align*}

Therefore, 23100 mod 11=22^{3^{100}} \bmod 11 = 2. Notice how each layer of the tower drops down one level: powers mod 1111 are governed by ϕ(11)=10\phi(11) = 10, and powers mod 1010 are governed by ϕ(10)=4\phi(10) = 4.

Fast Modular Exponentiation#

Reducing the exponent with Euler's Theorem is only half the job; the reduced exponent can still be uncomfortably large (anything up to ϕ(n)−1\phi(n) - 1), and multiplying one factor at a time takes forever. The fix is repeated squaring: square the base over and over, reducing modulo nn at every step, to obtain

a1,  a2,  a4,  a8,  a16,  …(modn),a^{1}, \; a^{2}, \; a^{4}, \; a^{8}, \; a^{16}, \; \dots \pmod n,

and then multiply together the powers matching the binary expansion of the exponent. This needs roughly log⁡2k\log_2 k multiplications instead of kk, and because we reduce mod nn after every squaring, the numbers involved never get big.

Example. Find the last two digits of 312343^{1234}.
The last two digits of a number are exactly its remainder mod 100100, so we want 31234 mod 1003^{1234} \bmod 100. First reduce the exponent using the totient function: gcd⁡(3,100)=1\gcd(3, 100) = 1 and

ϕ(100)=ϕ(4)ϕ(25)=2⋅20=40,\phi(100) = \phi(4)\phi(25) = 2 \cdot 20 = 40,

and since 1234=30⋅40+341234 = 30 \cdot 40 + 34, we have 31234≡334(mod100)3^{1234} \equiv 3^{34} \pmod{100}. Now 34=32+234 = 32 + 2, so we build up by repeated squaring;

32=9,34=92=81,38=812=6561≡61(mod100),316≡612(mod100)≡3721(mod100)≡21(mod100),332≡212(mod100)≡441(mod100)≡41(mod100),334=332⋅32≡41⋅9(mod100)≡369(mod100)≡69(mod100).\begin{align*} 3^{2} &= 9, \\ 3^{4} &= 9^{2} = 81, \\ 3^{8} &= 81^{2} = 6561 \equiv 61 \pmod{100}, \\ 3^{16} &\equiv 61^{2} \pmod{100} \\ &\equiv 3721 \pmod{100} \\ &\equiv 21 \pmod{100}, \\ 3^{32} &\equiv 21^{2} \pmod{100} \\ &\equiv 441 \pmod{100} \\ &\equiv 41 \pmod{100}, \\ 3^{34} &= 3^{32} \cdot 3^{2} \\ &\equiv 41 \cdot 9 \pmod{100} \\ &\equiv 369 \pmod{100} \\ &\equiv 69 \pmod{100}. \end{align*}

Therefore, the last two digits of 312343^{1234} are 6969.

For any "huge power mod nn" question, the routine is always the same: check gcd⁡(a,n)\gcd(a, n) first (if it is not 11, split nn into coprime pieces and quarantine the shared factor); factorise nn to compute ϕ(n)\phi(n); reduce the exponent modulo ϕ(n)\phi(n) — or modulo p−1p-1 when nn is prime; and finish off whatever exponent remains with repeated squaring, keeping an eye out for a handy −1-1 along the way.