Proofs of Fermat's little theorem

This article collects together a variety of proofs of Fermat's little theorem, which states that
 * $$a^p \equiv a \pmod p$$

for every prime number p and every integer a (see modular arithmetic).

Simplifications
Some of the proofs of Fermat's little theorem given below depend on two simplifications.

The first is that we may assume that $a$ is in the range $0 ≤ a ≤ p − 1$. This is a simple consequence of the laws of modular arithmetic; we are simply saying that we may first reduce $a$ modulo $p$. This is consistent with reducing $$a^p $$ modulo $p$, as one can check.

Secondly, it suffices to prove that
 * $$a^{p-1} \equiv 1 \pmod p$$

for $a$ in the range $1 ≤ a ≤ p − 1$. Indeed, if the previous assertion holds for such $a$, multiplying both sides by $a$ yields the original form of the theorem,
 * $$a^p \equiv a \pmod p $$

On the other hand, if $a = 0$ or $a = 1$, the theorem holds trivially.

Proof by counting necklaces
This is perhaps the simplest known proof, requiring the least mathematical background. It is an attractive example of a combinatorial proof (a proof that involves counting a collection of objects in two different ways).

The proof given here is an adaptation of Golomb's proof.

To keep things simple, let us assume that $a$ is a positive integer. Consider all the possible strings of $p$ symbols, using an alphabet with $a$ different symbols. The total number of such strings is $a^{p}$ since there are $a$ possibilities for each of $p$ positions (see rule of product).

For example, if $p = 5$ and $a = 2$, then we can use an alphabet with two symbols (say $A$ and $B$), and there are $2^{5} = 32$ strings of length 5:

We will argue below that if we remove the strings consisting of a single symbol from the list (in our example, $AAAAA$ and $AAAAB$), the remaining $AAABA$ strings can be arranged into groups, each group containing exactly $p$ strings. It follows that $AAABB$ is divisible by $p$.

Necklaces


Let us think of each such string as representing a necklace. That is, we connect the two ends of the string together and regard two strings as the same necklace if we can rotate one string to obtain the second string; in this case we will say that the two strings are friends. In our example, the following strings are all friends:

In full, each line of the following list corresponds to a single necklace, and the entire list comprises all 32 strings.

Notice that in the above list, each necklace with more than one symbol is represented by 5 different strings, and the number of necklaces represented by just one string is 2, i.e. is the number of distinct symbols. Thus the list shows very clearly why $AABAA$ is divisible by $AABAB$.

One can use the following rule to work out how many friends a given string $AABBA$ has:
 * If $AABBB$ is built up of several copies of the string $ABAAA$, and $ABAAB$ cannot itself be broken down further into repeating strings, then the number of friends of $ABABA$ (including $ABABB$ itself) is equal to the length of $ABBAA$.

For example, suppose we start with the string $ABBAB$, which is built up of several copies of the shorter string $ABBBA$. If we rotate it one symbol at a time, we obtain the following 3 strings:

There aren't any others because $ABB$ is exactly 3 symbols long and cannot be broken down into further repeating strings.

Completing the proof
Using the above rule, we can complete the proof of Fermat's little theorem quite easily, as follows. Our starting pool of $ABBBB$ strings may be split into two categories:
 * Some strings contain $p$ identical symbols. There are exactly $a$ of these, one for each symbol in the alphabet. (In our running example, these are the strings $AAAAA$ and $BBBBB$.)
 * The rest of the strings use at least two distinct symbols from the alphabet. If we can break up $S$ into repeating copies of some string $T$, the length of $T$ must divide the length of $S$. But since the length of $S$ is the prime $p$, the only possible length for $T$ is also $p$. Therefore, the above rule tells us that $S$ has exactly $p$ friends (including $S$ itself).

The second category contains $BAAAA$ strings, and they may be arranged into groups of $p$ strings, one group for each necklace. Therefore, $BAAAB$ must be divisible by $p$, as promised.

Proof using dynamical systems
This proof uses some basic concepts from dynamical systems.

We start by considering a family of functions Tn(x), where n ≥ 2 is an integer, mapping the interval [0, 1] to itself by the formula
 * $$T_n(x) = \begin{cases}

\{ nx \} & 0 \leq x < 1, \\ 1       & x = 1, \end{cases}$$ where {y} denotes the fractional part of y. For example, the function T3(x) is illustrated below:



A number x0 is said to be a fixed point of a function f(x) if f(x0) = x0; in other words, if f leaves x0 fixed. The fixed points of a function can be easily found graphically: they are simply the x coordinates of the points where the graph of f(x) intersects the graph of the line y = x. For example, the fixed points of the function T3(x) are 0, 1/2, and 1; they are marked by black circles on the following diagram:



We will require the following two lemmas.

Lemma 1. For any n ≥ 2, the function Tn(x) has exactly n fixed points.

Proof. There are 3 fixed points in the illustration above, and the same sort of geometrical argument applies for any n ≥ 2.

Lemma 2. For any positive integers n and m, and any 0 ≤ x ≤ 1,
 * $$T_m(T_n(x)) = T_{mn}(x).$$

In other words, Tmn(x) is the composition of Tn(x) and Tm(x).

Proof. The proof of this lemma is not difficult, but we need to be slightly careful with the endpoint x = 1. For this point the lemma is clearly true, since
 * $$T_m(T_n(1)) = T_m(1) = 1 = T_{mn}(1).$$

So let us assume that 0 ≤ x < 1. In this case,
 * $$T_n(x) = \{nx\} < 1, $$

so Tm(Tn(x)) is given by
 * $$T_m(T_n(x)) = \{m\{nx\}\}.$$

Therefore, what we really need to show is that
 * $$\{m\{nx\}\} = \{mnx\}.$$

To do this we observe that {nx} = nx − k, where k is the integer part of nx; then
 * $$\{m\{nx\}\} = \{mnx - mk\} = \{mnx\},$$

since mk is an integer.

Now let us properly begin the proof of Fermat's little theorem, by studying the function Tap(x). We will assume that a ≥ 2. From Lemma 1, we know that it has ap fixed points. By Lemma 2 we know that
 * $$T_{a^p}(x) = \underbrace{T_a(T_a( \cdots T_a(x) \cdots ))}_{p\text{ times}},$$

so any fixed point of Ta(x) is automatically a fixed point of Tap(x).

We are interested in the fixed points of Tap(x) that are not fixed points of Ta(x). Let us call the set of such points S. There are ap − a points in S, because by Lemma 1 again, Ta(x) has exactly a fixed points. The following diagram illustrates the situation for a = 3 and p = 2. The black circles are the points of S, of which there are 32 − 3 = 6.



The main idea of the proof is now to split the set S up into its orbits under Ta. What this means is that we pick a point x0 in S, and repeatedly apply Ta(x) to it, to obtain the sequence of points
 * $$ x_0, T_a(x_0), T_a(T_a(x_0)), T_a(T_a(T_a(x_0))), \ldots. $$

This sequence is called the orbit of x0 under Ta. By Lemma 2, this sequence can be rewritten as
 * $$ x_0, T_a(x_0), T_{a^2}(x_0), T_{a^3}(x_0), \ldots. $$

Since we are assuming that x0 is a fixed point of Tap(x), after p steps we hit Tap(x0) = x0, and from that point onwards the sequence repeats itself.

However, the sequence cannot begin repeating itself any earlier than that. If it did, the length of the repeating section would have to be a divisor of p, so it would have to be 1 (since p is prime). But this contradicts our assumption that x0 is not a fixed point of Ta.

In other words, the orbit contains exactly p distinct points. This holds for every orbit of S. Therefore, the set S, which contains ap − a points, can be broken up into orbits, each containing p points, so ap − a is divisible by p.

(This proof is essentially the same as the necklace-counting proof given above, simply viewed through a different lens: one may think of the interval [0, 1] as given by sequences of digits in base a (our distinction between 0 and 1 corresponding to the familiar distinction between representing integers as ending in ".0000..." and ".9999..."). Tan amounts to shifting such a sequence by n many digits. The fixed points of this will be sequences that are cyclic with period dividing n. In particular, the fixed points of Tap can be thought of as the necklaces of length p, with Tan corresponding to rotation of such necklaces by n spots.

This proof could also be presented without distinguishing between 0 and 1, simply using the half-open interval [0, 1); then Tn would only have n − 1 fixed points, but Tap − Ta would still work out to ap − a, as needed.)

Proof 1
This proof, due to Euler, uses induction to prove the theorem for all integers $BAABA$.

The base step, that $BAABB$, is trivial. Next, we must show that if the theorem is true for $BABAA$, then it is also true for $BABAB$. For this inductive step, we need the following lemma.

Lemma. For any integers $BABBA$ and $BABBB$ and for any prime $BBAAA$, $BBAAB$.

The lemma is a case of the freshman's dream. Leaving the proof for later on, we proceed with the induction.

Proof. Assume kp ≡ k (mod p), and consider (k+1)p. By the lemma we have


 * $$(k+1)^p \equiv k^p + 1^p \pmod{p}.$$

Using the induction hypothesis, we have that kp ≡ k (mod p); and, trivially, 1p = 1. Thus


 * $$(k+1)^p \equiv k + 1 \pmod{p},$$

which is the statement of the theorem for a = k+1. ∎

In order to prove the lemma, we must introduce the binomial theorem, which states that for any positive integer n,


 * $$(x+y)^n=\sum_{i=0}^n{n \choose i}x^{n-i}y^i,$$

where the coefficients are the binomial coefficients,


 * $${n \choose i}=\frac{n!}{i!(n-i)!},$$

described in terms of the factorial function, n! = 1×2×3×⋯×n.

Proof of Lemma. We consider the binomial coefficient when the exponent is a prime p:


 * $${p \choose i}=\frac{p!}{i!(p-i)!}$$

The binomial coefficients are all integers. The numerator contains a factor p by the definition of factorial. When 0 < i < p, neither of the terms in the denominator includes a factor of p (relying on the primality of p), leaving the coefficient itself to possess a prime factor of p from the numerator, implying that


 * $${p \choose i} \equiv 0 \pmod{p},\qquad 0 < i < p.$$

Modulo p, this eliminates all but the first and last terms of the sum on the right-hand side of the binomial theorem for prime p. ∎

The primality of p is essential to the lemma; otherwise, we have examples like


 * $${4 \choose 2} = 6,$$

which is not divisible by 4.

Proof 2
Using the Lemma, we have:


 * $$k^p \equiv ((k-1)+1)^p \equiv (k-1)^p + 1 \equiv ((k-2)+1)^p + 1 \equiv (k-2)^p + 2 \equiv \dots \equiv k \pmod{p}$$.

Proof using the multinomial expansion
The proof, which was first discovered by Leibniz (who did not publish it) and later rediscovered by Euler, is a very simple application of the multinomial theorem, which states


 * $$(x_1 + x_2 + \cdots + x_m)^n

= \sum_{k_1,k_2,\dots,k_m \atop k_1+k_2+\dots+k_m=n} {n \choose k_1, k_2, \ldots, k_m} x_1^{k_1} x_2^{k_2} \cdots x_m^{k_m}$$

where


 * $${n \choose k_1, k_2, \ldots, k_m} = \frac{n!}{k_1! k_2! \dots k_m!}$$

and the summation is taken over all sequences of nonnegative integer indices $BBABA$ such the sum of all $BBABB$ is $BBBAA$.

Thus if we express $BBBAB$ as a sum of 1s (ones), we obtain


 * $$a^p = (1 + 1 + ... + 1)^p

= \sum_{k_1,k_2,\dots,k_a \atop k_1+k_2+\dots+k_a=p} {p \choose k_1, k_2, \ldots, k_a}$$

Clearly, if $BBBBA$ is prime, and if $BBBBB$ is not equal to $AAAAA$ for any $BBBBB$, we have


 * $${p \choose k_1, k_2, \ldots, k_a} \equiv 0 \pmod p,$$

and if $a^{p} − a$ is equal to $a^{p} − a$ for some $ABCBAAC$ then


 * $${p \choose k_1, k_2, \ldots, k_a} = 1.$$

Since there are exactly $BCBAACA$ elements such that $CBAACAB$ for some $BAACABC$, the theorem follows.

(This proof is essentially a coarser-grained variant of the necklace-counting proof given earlier; the multinomial coefficients count the number of ways a string can be permuted into arbitrary anagrams, while the necklace argument counts the number of ways a string can be rotated into cyclic anagrams. That is to say, that the nontrivial multinomial coefficients here are divisible by $AACABCB$ can be seen as a consequence of the fact that each nontrivial necklace of length $ACABCBA$ can be unwrapped into a string in $CABCBAA$ many ways.

This multinomial expansion is also, of course, what essentially underlies the binomial theorem-based proof above)

Proof using power product expansions
An additive-combinatorial proof based on formal power product expansions was given by Giedrius Alkauskas. This proof uses neither the Euclidean algorithm nor the binomial theorem, but rather it employs formal power series with rational coefficients.

Proof as a particular case of Euler's theorem
This proof, discovered by James Ivory and rediscovered by Dirichlet, requires some background in modular arithmetic.

Let us assume that $AAAAAAA$ is positive and not divisible by $AAAAB$.

The idea is that if we write down the sequence of numbers

and reduce each one modulo $AAABA$, the resulting sequence turns out to be a rearrangement of

Therefore, if we multiply together the numbers in each sequence, the results must be identical modulo $AABAA$:
 * $$a \times 2a \times 3a \times \cdots \times (p-1)a \equiv 1 \times 2 \times 3 \times \cdots \times (p-1) \pmod p.$$

Collecting together the $ABAAA$ terms yields
 * $$a^{p-1} (p-1)! \equiv (p-1)! \pmod p.$$

Finally, we may “cancel out” the numbers $BAAAA$ from both sides of this equation, obtaining
 * $$a^{p-1} \equiv 1 \pmod p.$$

There are two steps in the above proof that we need to justify: We will prove these things below; let us first see an example of this proof in action.
 * Why the elements of the sequence ($$), reduced modulo $$, are a rearrangement of ($$), and
 * Why it is valid to “cancel” in the setting of modular arithmetic.

An example
If $AAAAB$ and $AAABA$, then the sequence in question is
 * $$3, 6, 9, 12, 15, 18;$$

reducing modulo 7 gives
 * $$3, 6, 2, 5, 1, 4,$$

which is just a rearrangement of
 * $$1, 2, 3, 4, 5, 6.$$

Multiplying them together gives
 * $$3 \times 6 \times 9 \times 12 \times 15 \times 18 \equiv 3 \times 6 \times 2 \times 5 \times 1 \times 4 \equiv 1 \times 2 \times 3 \times 4 \times 5 \times 6 \pmod 7;$$

that is,
 * $$3^6 (1 \times 2 \times 3 \times 4 \times 5 \times 6) \equiv (1 \times 2 \times 3 \times 4 \times 5 \times 6) \pmod 7.$$

Canceling out 1 × 2 × 3 × 4 × 5 × 6 yields
 * $$3^6 \equiv 1 \pmod 7, $$

which is Fermat's little theorem for the case $AABAA$ and $ABAAA$.

The cancellation law
Let us first explain why it is valid, in certain situations, to “cancel”. The exact statement is as follows. If $BAAAA$, $AAABB$, and $AABBA$ are integers, and $ABBAA$ is not divisible by a prime number $BBAAA$, and if

then we may “cancel” $BAAAB$ to obtain

Our use of this cancellation law in the above proof of Fermat's little theorem was valid because the numbers $AABAB$ are certainly not divisible by $ABABA$ (indeed they are smaller than $BABAA$).

We can prove the cancellation law easily using Euclid's lemma, which generally states that if a prime $ABAAB$ divides a product $BAABA$ (where $AABBB$ and $ABBBA$ are integers), then $BBBAA$ must divide $BBAAB$ or $BAABB$. Indeed, the assertion ($p$) simply means that $ABABB$ divides $BABBA$. Since $ABBAB$ is a prime which does not divide $BBABA$, Euclid's lemma tells us that it must divide $BABAB$ instead; that is, ($$) holds.

Note that the conditions under which the cancellation law holds are quite strict, and this explains why Fermat's little theorem demands that $$ is a prime. For example, $ABBBB$, but it is not true that $BBBBA$. However, the following generalization of the cancellation law holds: if $$, $$, $$, and $p$ are integers, if $u$ and $x$ are relatively prime, and if
 * $$ux \equiv uy \pmod z,$$

then we may “cancel” $y$ to obtain
 * $$x \equiv y \pmod z.$$

This follows from a generalization of Euclid's lemma.

The rearrangement property
Finally, we must explain why the sequence
 * $$a, 2a, 3a, \ldots, (p-1)a, $$

when reduced modulo p, becomes a rearrangement of the sequence
 * $$1, 2, 3, \ldots, p-1.$$

To start with, none of the terms $BBBAB$, $BBABB$, ..., $BABBB$ can be congruent to zero modulo $AAAAA$, since if $BBBBB$ is one of the numbers $32 − 2$, then $5$ is relatively prime with $S$, and so is $S$, so Euclid's lemma tells us that $T$ shares no factor with $T$. Therefore, at least we know that the numbers $S$, $S$, ..., $T$, when reduced modulo $S = ABBABBABBABB$, must be found among the numbers $T = ABB$.

Furthermore, the numbers $ABBABBABBABB$, $BBABBABBABBA$, ..., $BABBABBABBAB$ must all be distinct after reducing them modulo $a^{ p}$, because if
 * $$ka \equiv ma \pmod p, $$

where $a^{ p} − a$ and $a^{ p} − a$ are one of $a ≥ 0$, then the cancellation law tells us that
 * $$k \equiv m \pmod p. $$

Since both $0^{p} ≡ 0 (mod p)$ and $a = k$ are between $a = k + 1$ and $x$, they must be equal. Therefore, the terms $y$, $p$, ..., $(x + y)^{p} ≡ x^{p} + y^{p} (mod p)$ when reduced modulo $k_{1}, k_{2}, ..., k_{m}$ must be distinct. To summarise: when we reduce the $k_{i}$ numbers $n$, $a$, ..., $p$ modulo $k_{j}$, we obtain distinct members of the sequence $p$, $j$, ..., $k_{j}$. Since there are exactly $p$ of these, the only possibility is that the former are a rearrangement of the latter.

Applications to Euler's theorem
This method can also be used to prove Euler's theorem, with a slight alteration in that the numbers from $j$ to $a$ are substituted by the numbers less than and coprime with some number $z$ (not necessarily prime). Both the rearrangement property and the cancellation law (under the generalized form mentioned above) are still satisfied and can be utilized.

For example, if $k_{j} = p$, then the numbers less than $j$ and coprime with $p$ are $p$, $p$, $1, 2, ..., p − 1$, and $p$. Thus we have:
 * $$a \times 3a \times 7a \times 9a \equiv 1 \times 3 \times 7 \times 9 \pmod {10}. $$

Therefore,
 * $${a^{\varphi(10)}} \equiv 1 \pmod {10}. $$

Standard proof
This proof requires the most basic elements of group theory.

The idea is to recognise that the set $a$, with the operation of multiplication (taken modulo $p$), forms a group. The only group axiom that requires some effort to verify is that each element of $p$ is invertible. Taking this on faith for the moment, let us assume that $p$ is in the range $a$, that is, $1, 2, ..., p − 1$ is an element of $a = 3$. Let $p = 7$ be the order of $a = 3$, that is, $p = 7$ is the smallest positive integer such that $u$. Then the numbers $x$ reduced modulo $y$ form a subgroup of $u$ whose order is $p$ and therefore, by Lagrange's theorem, $u$ divides the order of $1, 2, ..., p − 1$, which is $p$. So $p$ for some positive integer $p$ and then
 * $$a^{p-1} \equiv a^{km} \equiv (a^k)^m \equiv 1^m \equiv 1 \pmod p. $$

To prove that every element $ab$ of $a$ is invertible, we may proceed as follows. First, $b$ is coprime to $p$. Thus Bézout's identity assures us that there are integers $a$ and $b$ such that $p$. Reading this equality modulo $ux − uy = u(x − y)$, we see that $p$ is an inverse for $u$, since $x − y$. Therefore, every element of $2×2 ≡ 2×5 (mod 6)$ is invertible. So, as remarked earlier, $2 ≡ 5 (mod 6)$ is a group.

For example, when $a$, the inverses of each element are given as follows:
 * {| border="1" cellspacing="0" cellpadding="8"


 * align="center" | $2a$
 * 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10
 * align="center" | $(p − 1)a$
 * 1 || 6 || 4 || 3 || 9 || 2 || 8 || 7 || 5 || 10
 * }
 * }

Euler's proof
If we take the previous proof and, instead of using Lagrange's theorem, we try to prove it in this specific situation, then we get Euler's third proof, which is the one that he found more natural. Let $p$ be the set whose elements are the numbers $k$ reduced modulo $1, 2, ..., p − 1$. If $k$, then $p$ and therefore $a$ divides $ka$. Otherwise, there is some $p$.

Let $a$ be the set whose elements are the numbers $2a$ reduced modulo $(p − 1)a$. Then $p$ has $1, 2, 3, ..., p − 1$ distinct elements because otherwise there would be two distinct numbers $a$ such that $2a$, which is impossible, since it would follow that $(p − 1)a$. On the other hand, no element of $p$ can be an element of $k$, because otherwise there would be numbers $m$ such that $1, 2, ..., p − 1$, and then $k$, which is impossible, since $m$.

So, the set $1$ has $p − 1$ elements. If it turns out to be equal to G, then $a$ and therefore $2a$ divides $(p − 1)a$. Otherwise, there is some $p$ and we can start all over again, defining $p − 1$ as the set whose elements are the numbers $a$ reduced modulo $2a$. Since $(p − 1)a$ is finite, this process must stop at some point and this proves that $p$ divides $1$.

For instance, if $2$ and $p − 1$, then, since we have $p − 1$ and $1$. Clearly, $p − 1$. Let $m = 10$ be an element of $m$; for instance, take $m$. Then, since we have $1$. Clearly, $3$. Let $7$ be an element of $9$; for instance, take $G = {1, 2, ..., p − 1}$. Then, since we have $p$. And now $G$.

Note that the sets $a$, $1 ≤ a ≤ p − 1$, and so on are in fact the cosets of $a$ in $G$.