1.2.3. Fermat’s Little Theorem

This section is based on part of [BUR11, Sec. 5.2].

Theorem 1.2.3.1 (Fermat’s Little Theorem).

Let pp be a prime and a∈ℤ{a\in\mathbb{Z}}. Then

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

The case a=0{a=0} is trivially true since 0p=0{0^{p}=0}. Suppose that a≠0{a\neq 0}. We may assume that a>0{a>0}. Indeed, if p=2{p=2}, then a≡−a(modp){a\equiv-a\pmod{p}}, while if pp is odd, then (−a)p=−ap{(-a)^{p}=-a^{p}} and

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

We prove the claim by induction on a≥1{a\geq 1}. The case a=1{a=1} is clear since 1p=1{1^{p}=1}. Inductively, let a≥1{a\geq 1} and suppose that ap≡a(modp){a^{p}\equiv a\pmod{p}}. We need to prove that

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

By the Binomial Theorem,

(a+1)p=∑k=0p(pk)⁢ap−k⁢1k=∑k=0p(pk)⁢ap−k.(a+1)^{p}=\sum_{k=0}^{p}{p\choose k}a^{p-k}1^{k}=\sum_{k=0}^{p}{p\choose k}a^{% p-k}.

If k≠0{k\neq 0} and k≠p{k\neq p}, then

(pk)=p!(p−k)!⁢k!{p\choose k}=\frac{p!}{(p-k)!k!}

is divisible by pp (notice that pp divides k!⁢(pk)=(p−k+1)⁢⋯⁢(p−1)⁢p{k!{p\choose k}=(p-k+1)\cdots(p-1)p} but pp does not divide k!k!, and apply Theorem 1.1.4.2). Hence, (pk)≡0(modp){{p\choose k}\equiv 0\pmod{p}} and therefore

(a+1)p≡(p0)⁢ap+(pp)⁢1(modp).(a+1)^{p}\equiv{p\choose 0}a^{p}+{p\choose p}1\pmod{p}.

Since (p0)=(pp)=1{{p\choose 0}={p\choose p}=1}, the induction hypothesis ap≡a(modp){a^{p}\equiv a\pmod{p}} yields the required congruence. ∎

Corollary 1.2.3.2.

Let pp be a prime and a∈ℤ{a\in\mathbb{Z}} be such that gcd⁡(a,p)=1{\gcd(a,p)=1}. Then

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

By Fermat’s Little Theorem (Theorem 1.2.3.1), the congruence ap≡a(modp){a^{p}\equiv a\pmod{p}} holds. Using Corollary 1.2.1.11, we may cancel aa on both sides to obtain

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

As stated, Fermat’s Little Theorem (Theorem 1.2.3.1) and its corollary (Corollary 1.2.3.2) are false for arbitrary composite moduli: for example, let n=4{n=4} and a=3{a=3}. Then

32≡1(mod4),33≡3(mod4)and34≡1(mod4),3^{2}\equiv 1{\pmod{4}},\qquad 3^{3}\equiv 3{\pmod{4}}\qquad\text{and}\qquad 3% ^{4}\equiv 1{\pmod{4}},

but 1≢3(mod4){1\not\equiv 3{\pmod{4}}}. There is, however, a suitable generalisation of Corollary 1.2.3.2 that we study later in Section 1.2.6.

Fermat’s Little Theorem (Corollary 1.2.3.2) can be used to simplify certain calculations.

Example 1.2.3.4.

Let p=5{p=5}. Suppose that one is interested in solving the linear congruence

2⁢x≡1337(mod5).2x\equiv 13^{37}{\pmod{5}}.

Since 13≡3(mod5){13\equiv 3{\pmod{5}}}, this congruence is equivalent to the congruence

2⁢x≡337(mod5).2x\equiv 3^{37}{\pmod{5}}.

Also, since 37=9⋅4+1{37=9\cdot 4+1}, we have

337=(34)9⋅3.3^{37}=(3^{4})^{9}\cdot 3.

Since gcd⁡(5,3)=1{\gcd(5,3)=1}, Fermat’s Little Theorem (Corollary 1.2.3.2) yields the congruence

34≡1(mod5).3^{4}\equiv 1{\pmod{5}}.

Hence,

337≡3(mod5).3^{37}\equiv 3{\pmod{5}}.

Therefore, the original linear congruence is equivalent to the linear congruence

2⁢x≡3(mod5),2x\equiv 3{\pmod{5}},

which clearly has x=4{x=4} as a solution, and this solution is unique modulo 55 (Corollary 1.2.2.7).

Fermat’s Little Theorem (Corollary 1.2.3.2) provides a necessary condition for an integer nn to be prime without exhibiting a proper divisor: if nn is prime, then for every a∈ℤ{a\in\mathbb{Z}} with gcd⁡(a,n)=1{\gcd(a,n)=1} we must have an−1≡1(modn){a^{n-1}\equiv 1{\pmod{n}}}. This condition can sometimes be checked by hand, as we show in the following example.

Example 1.2.3.5.

Not covered in the lectureLet n=143{n=143} and a=3{a=3}. By Proposition 1.2.1.12, we have gcd⁡(3,143)=1{\gcd(3,143)=1}. If 143143 is prime, by Fermat’s Little Theorem (Corollary 1.2.3.2) we must have

3142≡1(mod143).3^{142}\equiv 1{\pmod{143}}.

We show that this is not the case. We first observe that 35=243{3^{5}=243} and therefore 243≡100(mod143){243\equiv 100{\pmod{143}}}. Using that 3142=(35)28⋅32{3^{142}=(3^{5})^{28}\cdot 3^{2}}, we obtain the congruence

3142≡10028⋅32(mod143).3^{142}\equiv 100^{28}\cdot 3^{2}{\pmod{143}}.

Moreover, 1002=70⋅143−10{100^{2}=70\cdot 143-10} since 70⋅143=10⁢010{70\cdot 143=10~{}010}, that is,

1002≡−10(mod143).100^{2}\equiv-10{\pmod{143}}.

Multiplying the latter congruence by 100100, we obtain

1003≡−1000(mod143).100^{3}\equiv-1000{\pmod{143}}.

We now notice that 7⋅143=1001=1000+1{7\cdot 143=1001=1000+1} and therefore

−1000≡1(mod143).-1000\equiv 1{\pmod{143}}.

We summarise our computations in the congruence

1003≡1(mod143).100^{3}\equiv 1{\pmod{143}}.

Using this last congruence and the factorisation 10028⋅32=(1003)9⋅100⋅32{100^{28}\cdot 3^{2}=(100^{3})^{9}\cdot 100\cdot 3^{2}}, we obtain the simplified congruence

3142≡100⋅32(mod143).3^{142}\equiv 100\cdot 3^{2}{\pmod{143}}.

Finally, 900=100⋅32=6⋅143+42{900=100\cdot 3^{2}=6\cdot 143+42} and therefore 900≡42(mod143){900\equiv 42{\pmod{143}}}. Combining the above, we obtain the congruence

3142≡42(mod143).3^{142}\equiv 42{\pmod{143}}.

Since 42≢1(mod143){42\not\equiv 1{\pmod{143}}}, the integer 143143 is composite (otherwise we would obtain a contradiction to Fermat’s Little Theorem (Corollary 1.2.3.2)). Indeed, 143=11⋅13{143=11\cdot 13}. The factorisation of 143143 can be found more directly by trial division since 143<12{\sqrt{143}<12}; see Corollary 1.1.4.11.

Remark 1.2.3.6.

We come back to Fermat’s Little Theorem (Theorem 1.2.3.1) and its corollary (Corollary 1.2.3.2) in Section 1.4.1.