Fermat's Little Theorem

It is a fundamental result in number theory.

Theorem. Given a prime pp, for every positive integer a∈N+a \in \mathbb{N}^+, if a≠0(modp)a \neq 0 \pmod p, then

ap−1=1(modp). a^{p - 1} = 1 \quad \pmod p.

It suffices to prove the theorem for numbers in Fp+≐{1,2,...,p−1}\mathbb{F}_p^+ \doteq \{ 1, 2, ..., p - 1\}.

Proof One

Define the function f:Fp+→Fp+f : \mathbb{F}_p^+ \rightarrow \mathbb{F}_p^+ to be

f(x)=ax(modp). f(x) = ax \pmod p.

For every x≠x′∈Fp+x \neq x' \in \mathbb{F}_p^+, ax≠ax′mod  pax \neq ax' \mod p, otherwise (x−x′)∣p(x - x') \mid p. Hence ff is a bijection and

{f(1),f(2),...,f(p−1)}=Fp+. \{f(1), f(2), ..., f(p - 1)\} = \mathbb{F}_p^+.

Now we take product over all number in these two sets to obtain

(p−1)!=∏i=1p−1(ai)=ap−1(p−1)!(modp) (p - 1)! = \prod_{i = 1}^{p - 1} (ai) = a^{p - 1} (p - 1)! \pmod p

As pp is a prime, every non-zero integer has a multiplicative inverse mod pp. Therefore we can cancel out (p−1)!(p - 1)! from both side and get

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

□\square

Remark: Fermat-Euler theorem relaxes the condition of pp being prime. It states that ∀p,a∈N+\forall p, a \in \mathbb{N}^+ with gcd⁡(a,p)=1\gcd(a, p) = 1 (i.e., aa and pp are coprime), we have

aφ(p)=1(modp), a^{\varphi(p)} = 1 \pmod p,

where φ(p)\varphi(p) is the number of elements that are coprime to pp in {1,2,..,p−1}\{1, 2, .., p - 1\}. When pp is prime, φ(p)=p−1\varphi(p) = p - 1.

The proof for the theorem is similar to the previous one. Let

S⊆{1,2,..,p−1} S \subseteq \{1, 2, .., p - 1\}

be the set of numbers coprime to pp and define f:S→Sf: S \rightarrow S to be

f(x)=ax(modp) f(x) = ax \pmod p

which is a bijection. Therefore,

∏x∈Sx=aφ(p)∏x∈Sx(modp) \prod_{x \in S} x = a^{\varphi(p)} \prod_{x \in S} x \pmod p

Cancelling out the factor ∏x∈Sx\prod_{x \in S} x from both side gives the desired result.

Proof Two

This proof uses the binomial expansion as follows:

(a+1)p=ap+(p1)ap−1+...+(pp−1)a+1(modp)=ap+1(modp)=(a−1)p+1+1(modp)=...=1+1+...+1⏟a+1(modp)=a+1(modp) \begin{aligned} (a + 1)^p &= a^p + \binom{p}{1} a^{p - 1} + ... + \binom{p}{p - 1} a + 1 &\pmod p \\ &= a^{p} + 1 &\pmod p \\ &= (a - 1)^{p} + 1 + 1 &\pmod p \\ &= ... \\ &= \begin{matrix} \underbrace{1 + 1 + ... + 1} \\ a + 1 \end{matrix} &\pmod p \\ &= a + 1 &\pmod p \end{aligned}

The second equality holds since for 1≤i≤p−11 \le i \le p - 1, the coefficient (pi)=p(p−1)...(p−i+1)i!\binom{p}{i} = \frac{p (p - 1) ... (p - i + 1)}{i !} divides pp, as pp is relative prime to all integers 1,2,...,i1, 2, ..., i. The third equality follows from respectively applying the previous expansion. Finally, we get Fermat's Little Theorem by multiplying both sides of (a+1)p=(a+1)(modp)(a + 1)^p = (a + 1) \pmod p by the inverse of a+1a + 1 module pp.

Proof Three

Let a∈Fp+a \in \mathbb{F}_p^+. Consider the following numbers in Fp+\mathbb{F}_p^+:

a,a2,a3,a4,a5,... a, a^2, a^3, a^4, a^5, ...

As there are only p−1p - 1 distinct numbers in Fp+\mathbb{F}_p^+, it holds that there exits a smallest h∈N+h \in \mathbb{N}_+, such that

ah=1(modp). a^h = 1 \pmod p.

Now consider the following procedure:

  1. U←Fp+;\mathcal{U} \leftarrow \mathbb{F}_p^+;
  2. C←{},i←0\mathcal{C} \leftarrow \{ \}, i \leftarrow 0;
  3. while U≠∅\mathcal{U} \neq \emptyset do:
  4. \qquad i←i+1i \leftarrow i + 1;
  5. \qquad let bb be an element in U\mathcal{U};
  6. \qquad let Si←{b,ab,a2b,...,ah−1b}S_i \leftarrow \{ b, ab, a^2b, ..., a^{h - 1} b\};
  7. \qquad U←U∖Si\mathcal{U} \leftarrow \mathcal{U} \setminus S_i;
  8. \qquad C←C∪{Si}\mathcal{C} \leftarrow \mathcal{C} \cup \{ S_i \};

Suppose that C={S1,...,Sm}\mathcal{C} = \{ S_1, ..., S_m \}. We claim that

  • The elements in SiS_i are distinct. Otherwise,

    akb=aℓb(modp)⟹ak−ℓb=0(modp), a^k b = a^\ell b \pmod p \Longrightarrow a^{k - \ell} b= 0 \pmod p,

    a contradiction. If follows that ∣Si∣=h|S_i| = h.

  • For i≠ji \neq j, Si∩Sj=∅S_i \cap S_j = \emptyset. Otherwise, let b∈Si∩Sjb \in S_i \cap S_j. Then every element in Si∪SjS_i \cup S_j can be expressed as akba^k b for some integer kk.

It follows that C\mathcal{C} is a partition of Fp+\mathbb{F}_p^+. As each set in C\mathcal{C} has the same size, we have h∣(p−1)h \mid (p - 1) and

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

□\square