In the last topic we dealt with linear congruences like ax≡c(modn). The natural next step is congruences involving higher powers of x, such as x2≡c(modn); but before we can say anything about those, we need to understand how powers behave in Zn in the first place.
Consider the powers of every element of Z7:
xk
k=1
2
3
4
5
6
7
…
0k
0
0
0
0
0
0
0
…
1k
1
1
1
1
1
1
1
…
2k
2
4
1
2
4
1
2
…
3k
3
2
6
4
5
1
3
…
4k
4
2
1
4
2
1
4
…
5k
5
4
6
2
3
1
5
…
6k
6
1
6
1
6
1
6
…
Notice how much structure is hiding in here:
In every row except 0k, a 1 eventually appears.
Column 6 is (almost) all 1's, and column 7 is identical to column 1; raising to the power 7 does nothing in Z7.
The first 1 in each row appears in column 1,2,3 or 6; always a divisor of 6.
The cycle of entries in row xk is the reverse of the cycle in row (x−1)k.
Going down the rows, the entries of column 2 cycle with length 3, and the entries of column 3 cycle with length 2.
Running the same experiment in Z11 tells the same story: column 10 is (almost) all 1's, column 11 repeats column 1, and the first 1 of each row lands in column 1,2,5 or 10 — again, always a divisor of 10.
So in Zp it looks like ap−1=1 for every a=0, and ap=a for absolutely every a. This is not a coincidence.
Fermat's Little Theorem For any prime p and any integer a, we have
ap≡a(modp).
Similarly, for any prime p and any integer a such that p∤a, we have
ap−1≡1(modp).
Basically, modulo a prime, raising to the power p is invisible, and every residue that is not a multiple of p resets to 1 after exactly p−1 steps; this is precisely the "column 6 is all 1's, column 7 copies column 1" pattern from the tables. For p∤a the two forms are equivalent (multiply the second by a to get the first), but they are not interchangeable in general. The second form requires p∤a; if p∣a then a≡0(modp) and no power of it will ever be 1.
Proof.
If p∣a, then a≡0(modp), so ap≡0≡a(modp) and the first form holds trivially.
So suppose that p∤a, and consider the p−1 numbers
a,2a,3a,…,(p−1)a(modp).
None of these is 0 modulo p; if p∣ia then, since p is prime, p∣i or p∣a, and both are impossible for 1≤i≤p−1. They are also pairwise distinct modulo p; if ia≡ja(modp) then p∣(i−j)a, and since p∤a we must have p∣i−j, which forces i=j as ∣i−j∣≤p−2<p.
So the list above is just 1,2,…,p−1 rearranged. Multiplying everything together,
Since p is prime, it divides none of 1,2,…,p−1, so gcd((p−1)!,p)=1 and (p−1)! is a unit modulo p; cancelling it from both sides gives
ap−1≡1(modp).■
To see the rearrangement step in action, take p=7 and a=3;
{3,6,9,12,15,18}≡{3,6,2,5,1,4}(mod7),
which really is {1,2,3,4,5,6} shuffled.
Example. Find 5097mod97.
Here 97 is prime, so the first form of Fermat's Little Theorem applies to every integer with no coprimality check needed;
5097≡50(mod97).
Therefore, 5097mod97=50; the exponent matching the modulus means we get the answer for free.
Example. Find 99100mod101, and hence find 99909mod101.
Since 101 is prime and 101∤99, the second form gives
99100≡1(mod101),
so 99100mod101=1. For the second power, we peel off as many blocks of 100 from the exponent as possible; each block collapses to 1. Writing 909=9⋅100+9 and noticing that 99≡−2(mod101),
The "peel off blocks of p−1" trick from the last example is worth stating as a theorem in its own right.
Note
Theorem For any prime p and integer a such that p∤a, we have for all integers k that
ak≡a(kmodp−1)(modp).
Proof. By the division theorem, write k=q(p−1)+r where r=kmod(p−1), so 0≤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).■
Basically, when working modulo a prime p, the bases live in Zp but the exponents live in Zp−1. It is important to remember that exponents get reduced modulo p−1, not modulo p; reducing an exponent mod p is the classic exam mistake.
p∤a⟹ak≡akmod(p−1)(modp).
Example. Find 31000mod7.
Here 7 is prime and 7∤3, so exponents can be reduced modulo 7−1=6;
Fermat's Little Theorem is strictly about prime moduli. To see what breaks otherwise, here is the power table for Z9:
xk
k=1
2
3
4
5
6
7
…
0k
0
0
0
0
0
0
0
…
1k
1
1
1
1
1
1
1
…
2k
2
4
8
7
5
1
2
…
3k
3
0
0
0
0
0
0
…
4k
4
7
1
4
7
1
4
…
5k
5
7
8
4
2
1
5
…
6k
6
0
0
0
0
0
0
…
7k
7
4
1
7
4
1
7
…
8k
8
1
8
1
8
1
8
…
We can observe some familiar patterns, and one new pathology:
A 1 eventually appears in every row except0k, 3k and 6k.
Column 6 is mostly 1's, and column 7 is similar to column 1.
The first 1 in each row appears in column 1,2,3 or 6.
The rows for 3 and 6 collapse to 0 and never recover.
The rows that die are exactly the elements sharing a factor with 9; indeed 32=9≡0, and once you hit 0 you stay there. The rows that do return to 1 are exactly the units of Z9, and there are 6 of them — which explains why column 6 plays the role that column p−1 played in the prime tables. What we need is a function that counts these "good" elements for an arbitrary modulus.
Definition
The (Euler) totient function (or Euler's phi function) is the function ϕ that for any positive integer n outputs the number of positive integers less than or equal to n that are coprime with n. That is,
ϕ(n)=∣{a∈Z:1≤a≤n and gcd(a,n)=1}∣.
Recall from Modular Rings and Units that a∈Zn∗⟺gcd(a,n)=1; so ϕ(n) is exactly the number of units in Zn,
ϕ(n)=∣Zn∗∣.
Basically, ϕ(n) counts how many residues mod n you are allowed to divide by; equivalently, how many rows of the power table will eventually return to 1.
Example. Find the following values.
ϕ(7)=6; since 7 is prime, all of 1,2,3,4,5,6 are coprime with it.
ϕ(9)=6; we only delete the multiples of 3, leaving {1,2,4,5,7,8} — matching the six "surviving" rows in the Z9 table above.
ϕ(12)=4; the survivors are {1,5,7,11}.
ϕ(15)=8; the survivors are {1,2,4,7,8,11,13,14}.
For any prime number p, we have ϕ(p)=p−1; every one of 1,2,…,p−1 is coprime with p.
Notice that the p−1 in Fermat's Little Theorem was really ϕ(p) in disguise; this is the observation that Euler generalised.
Computing ϕ(n) straight from the definition means checking a gcd for every single a≤n, which is hopeless for large n. The following two results reduce the whole computation to knowing the prime factorisation of n.
Note
Theorem For any prime p and k∈Z+,
ϕ(pk)=pk−pk−1=pk−1(p−1).
Proof. The only prime factor of pk is p, so gcd(a,pk)>1 if and only if p∣a. Hence we take all pk integers in 1,2,…,pk and delete the multiples of p, which are
p,2p,3p,…,pk−1⋅p;
there are exactly pk−1 of them. Everything remaining is coprime with pk, so ϕ(pk)=pk−pk−1. ■
Basically, exactly 1 in every p consecutive integers is a multiple of p, so a prime power keeps a (1−p1) share of its residues as units.
As a sanity check for the first one, the odd residues {1,3,5,7} are precisely the units of Z8; there are indeed 4 of them. Therefore, ϕ(8)=4 and ϕ(125)=100.
Proof. First, notice that gcd(a,mn)=1 if and only if gcd(a,m)=1 and gcd(a,n)=1; any prime dividing both a and mn must divide m or n, and conversely any common prime factor of a and m (or n) also divides mn.
Now write the integers 1,2,…,mn in a grid with m columns, so that column c contains
c,m+c,2m+c,…,(n−1)m+c.
Every entry of column c is congruent to c modulo m; so the columns containing numbers coprime with m are exactly the columns with gcd(c,m)=1, and there are ϕ(m) of these.
Fix one such column and reduce its n entries modulo n. They are pairwise distinct; if im+c≡jm+c(modn) then n∣(i−j)m, and since gcd(m,n)=1 we get n∣i−j, forcing i=j as ∣i−j∣<n. So the n entries of the column hit every residue modulo n exactly once, and hence exactly ϕ(n) of them are coprime with n.
Counting: ϕ(m) good columns, each contributing ϕ(n) entries coprime with both m and n; i.e. coprime with mn. Therefore ϕ(mn)=ϕ(m)ϕ(n). ■
To see this concretely with m=3 and n=4, lay out 1 to 12:
c=1
c=2
c=3
1
2
3
4
5
6
7
8
9
10
11
12
Column 3 is dead (everything shares the factor 3), while columns 1 and 2 each contain every residue mod 4 once, hence ϕ(4)=2 units each. That gives ϕ(3)ϕ(4)=2⋅2=4 units in total, and indeed Z12∗={1,5,7,11}.
It is important to note that ϕ is only multiplicative when gcd(m,n)=1; for instance ϕ(4)=2 but ϕ(2)ϕ(2)=1⋅1=1. Always split n into coprime pieces (i.e. into distinct prime powers), never arbitrary factors.
Example. Find ϕ(360).
Factorising and splitting into pairwise coprime prime powers,
Basically, each distinct prime factor independently deletes its own p1 share of the residues. Notice that the exponents in the factorisation never appear in the product; only which primes divide n matters, with the exponents hiding inside the leading factor of n.
We can now generalise Fermat's Little Theorem to any modulus, with ϕ(n) stepping into the role of p−1.
Note
Euler's Theorem For any a∈Z and any n∈Z+ such that gcd(a,n)=1, we have
aϕ(n)≡1(modn).
Equivalently, for any n∈Z+, we have that for all a∈Zn⋆,
aϕ(n)=1 in Zn.
Setting n=p prime recovers Fermat's Little Theorem exactly, since ϕ(p)=p−1 and gcd(a,p)=1 is the same condition as p∤a. Basically, the units of Zn all return to 1 after ϕ(n) steps — which is why column 6 worked in the Z9 table (ϕ(9)=6) even though 9 is not prime. Unlike Fermat's Little Theorem, there is no "first form" here: if gcd(a,n)=1 then Euler's Theorem says nothing at all about a.
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) be the elements of Zn∗; by definition of ϕ there are exactly ϕ(n) of them. Since gcd(a,n)=1, a is itself a unit, and products of units are units, so each ari∈Zn∗. Moreover the ari are pairwise distinct in Zn; if ari≡arj(modn), multiplying both sides by a−1 (which exists as a is a unit) gives ri≡rj, so i=j. Hence ar1,ar2,…,arϕ(n) is just r1,r2,…,rϕ(n) rearranged. Multiplying everything together,
Therefore, 544mod18=7 and 577mod18=11. Spotting a power congruent to −1 is one of the best shortcuts available; it halves the effective cycle length.
Example. Find 2100mod12.
The tempting move is: ϕ(12)=4 and 100≡0(mod4), "so" 2100≡20=1(mod12). This is wrong; gcd(2,12)=2=1, so Euler's Theorem does not apply and you are not allowed to reduce the exponent. Indeed every positive power of 2 is even, so none of them can possibly be congruent to 1 modulo 12.
The correct approach is to split 12 into coprime pieces, 12=4⋅3, and study each separately;
Now we need the unique x with 0≤x<12 satisfying x≡0(mod4) and x≡1(mod3); checking the candidates 0,4,8, only x=4 works. Therefore, 2100mod12=4. Notice that Euler was still useful on the coprime piece (as gcd(2,3)=1); the trick is to quarantine the shared factor first.
Euler's Theorem also hands us a formula for inverses. Since
a⋅aϕ(n)−1=aϕ(n)≡1(modn),
we immediately get
gcd(a,n)=1⟹a−1≡aϕ(n)−1(modn).
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−1 in Z30.
We have gcd(7,30)=1 and
Just as with primes, the real day-to-day use of Euler's Theorem is collapsing huge exponents.
Note
Theorem For any n∈Z+ and a∈Z such that gcd(a,n)=1, we have for all k∈Z that
ak≡a(kmodϕ(n))(modn).
Proof. By the division theorem, write k=qϕ(n)+r where r=kmodϕ(n), so 0≤r<ϕ(n). Then
ak=aqϕ(n)+r=(aϕ(n))q⋅ar≡1q⋅ar(modn), (by Euler’s Theorem, as gcd(a,n)=1)≡ar(modn).■
So when gcd(a,n)=1, bases live in Zn but exponents live in Zϕ(n);
gcd(a,n)=1⟹ak≡akmodϕ(n)(modn).
Exponents are reduced modulo ϕ(n), never modulo n itself.
Example. Find 23100mod11.
This is a tower of powers, so we work from the outside in. The outer modulus 11 is prime with gcd(2,11)=1, so the exponent 3100 only matters modulo ϕ(11)=10. That turns the problem into finding 3100mod10; and since gcd(3,10)=1 with ϕ(10)=ϕ(2)ϕ(5)=4, that exponent only matters modulo 4;
Therefore, 23100mod11=2. Notice how each layer of the tower drops down one level: powers mod 11 are governed by ϕ(11)=10, and powers mod 10 are governed by ϕ(10)=4.
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), and multiplying one factor at a time takes forever. The fix is repeated squaring: square the base over and over, reducing modulo n at every step, to obtain
a1,a2,a4,a8,a16,…(modn),
and then multiply together the powers matching the binary expansion of the exponent. This needs roughly log2k multiplications instead of k, and because we reduce mod n after every squaring, the numbers involved never get big.
Example. Find the last two digits of 31234.
The last two digits of a number are exactly its remainder mod 100, so we want 31234mod100. First reduce the exponent using the totient function: gcd(3,100)=1 and
ϕ(100)=ϕ(4)ϕ(25)=2⋅20=40,
and since 1234=30⋅40+34, we have 31234≡334(mod100). Now 34=32+2, so we build up by repeated squaring;
For any "huge power mod n" question, the routine is always the same: check gcd(a,n) first (if it is not 1, split n into coprime pieces and quarantine the shared factor); factorise n to compute ϕ(n); reduce the exponent modulo ϕ(n) — or modulo p−1 when n is prime; and finish off whatever exponent remains with repeated squaring, keeping an eye out for a handy −1 along the way.