Proofs of Fermat's little theorem


This article collects together a variety of proofs of Fermat's little theorem, which states that
for every prime number p and every integer a.

Simplifications

Some of the proofs of Fermat's little theorem given below depend on two simplifications.
The first is that we may assume that is in the range. This is a simple consequence of the laws of modular arithmetic; we are simply saying that we may first reduce modulo . This is consistent with reducing modulo , as one can check.
Secondly, it suffices to prove that
for in the range. Indeed, if the previous assertion holds for such, multiplying both sides by yields the original form of the theorem,
On the other hand, if, the theorem holds trivially.

Combinatorial proofs

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.
The proof given here is an adaptation of Golomb's proof.
To keep things simple, let us assume that is a positive integer. Consider all the possible strings of symbols, using an alphabet with different symbols. The total number of such strings is, since there are possibilities for each of positions.
For example, if and, then we can use an alphabet with two symbols, and there are strings of length 5:
We will argue below that if we remove the strings consisting of a single symbol from the list, the remaining strings can be arranged into groups, each group containing exactly strings. It follows that is divisible by .

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:
Similarly, each line of the following list corresponds to a single necklace.
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 is divisible by.
One can use the following rule to work out how many friends a given string has:
For example, suppose we start with the string, which is built up of several copies of the shorter string. If we rotate it one symbol at a time, we obtain the following 3 strings:
There aren't any others, because 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 strings may be split into two categories:
The second category contains strings, and they may be arranged into groups of strings, one group for each necklace. Therefore, must be divisible by, as promised.

Proof using dynamical systems

This proof uses some basic concepts from dynamical systems.
We start by considering a family of functions Tn, where n ≥ 2 is an integer, mapping the interval to itself by the formula
where denotes the fractional part of y. For example, the function T3 is illustrated below:
A number x0 is said to be a fixed point of a function f if f = 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 intersects the graph of the line y = x. For example, the fixed points of the function T3 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 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,
In other words, Tmn is the composition of Tn and Tm.
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
So let us assume that 0 ≤ x < 1. In this case,
so Tm is given by
Therefore, what we really need to show is that
To do this we observe that = nxk, where k is the integer part of nx; then
since mk is an integer.
Now let us properly begin the proof of Fermat's little theorem, by studying the function Tap. We will assume that a ≥ 2. From Lemma 1, we know that it has ap fixed points. By Lemma 2 we know that
so any fixed point of Ta is automatically a fixed point of Tap.
We are interested in the fixed points of Tap that are not fixed points of Ta. Let us call the set of such points S. There are apa points in S, because by Lemma 1 again, Ta 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 to it, to obtain the sequence of points
This sequence is called the orbit of x0 under Ta. By Lemma 2, this sequence can be rewritten as
Since we are assuming that x0 is a fixed point of Ta p, after p steps we hit Tap = 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. 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 apa points, can be broken up into orbits, each containing p points, so apa is divisible by p.
; then Tn would only have n − 1 fixed points, but TapTa would still work out to apa, as needed.)

Multinomial proofs

Proof using the binomial theorem

This proof, due to Euler, uses induction to prove the theorem for all integers.
The base step, that, is trivial. Next, we must show that if the theorem is true for, then it is also true for. For this inductive step, we need the following lemma.
Lemma. For any integers and and for any prime,.
The lemma is a case of the freshman's dream. Leaving the proof for later on, we proceed with the induction.
Proof. Assume kpk, and consider p. By the lemma we have
Using the induction hypothesis, we have that kpk ; and, trivially, 1p = 1. Thus
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,
where the coefficients are the binomial coefficients,
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:
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, leaving the coefficient itself to possess a prime factor of p from the numerator, implying that
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
which is not divisible by 4.

Proof using the multinomial expansion

The proof, which was first discovered by Leibniz and later rediscovered by Euler, is a very simple application of the multinomial theorem which is brought here for the sake of simplicity.
The summation is taken over all sequences of nonnegative integer indices through such the sum of all is.
Thus if we express as a sum of 1s, we obtain
Clearly, if is prime, and if is not equal to for any, we have
and
if is equal to for some.
Since there are exactly elements such that, the theorem follows.

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 is positive and not divisible by.
The idea is that if we write down the sequence of numbers
and reduce each one modulo, 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 :
Collecting together the terms yields
Finally, we may “cancel out” the numbers from both sides of this equation, obtaining
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.

An example

If and, then the sequence in question is
reducing modulo 7 gives
which is just a rearrangement of
Multiplying them together gives
that is,
Canceling out 1 × 2 × 3 × 4 × 5 × 6 yields
which is Fermat's little theorem for the case and .

The cancellation law

Let us first explain why it is valid, in certain situations, to “cancel”. The exact statement is as follows. If,, and are integers, and is not divisible by a prime number, and if
then we may “cancel” to obtain
Our use of this cancellation law in the above proof of Fermat's little theorem was valid, because the numbers are certainly not divisible by .
We can prove the cancellation law easily using Euclid's lemma, which generally states that if a prime divides a product , then must divide or. Indeed, the assertion simply means that divides. Since is a prime which does not divide, Euclid's lemma tells us that it must divide 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,, but it is not true that. However, the following generalization of the cancellation law holds: if,,, and are integers, if and are relatively prime, and if
then we may “cancel” to obtain
This follows from a generalization of Euclid's lemma.

The rearrangement property

Finally, we must explain why the sequence
when reduced modulo p, becomes a rearrangement of the sequence
To start with, none of the terms,,..., can be congruent to zero modulo, since if is one of the numbers, then is relatively prime with, and so is, so Euclid's lemma tells us that shares no factor with. Therefore, at least we know that the numbers,,...,, when reduced modulo, must be found among the numbers.
Furthermore, the numbers,,..., must all be distinct after reducing them modulo, because if
where and are one of, then the cancellation law tells us that
Since both and are between and, they must be equal. Therefore, the terms,,..., when reduced modulo must be distinct.
To summarise: when we reduce the numbers,,..., modulo, we obtain distinct members of the sequence, , ..., . Since there are exactly 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 to are substituted by the numbers less than and coprime with some number . Both the rearrangement property and the cancellation law are still satisfied and can be utilized.
For example, if, then the numbers less than and coprime with are,,, and. Thus we have:
Therefore,

Proof as a corollary of Euler's criterion

Proofs using group theory

Standard proof

This proof requires the most basic elements of group theory.
The idea is to recognise that the set, with the operation of multiplication, forms a group. The only group axiom that requires some effort to verify is that each element of is invertible. Taking this on faith for the moment, let us assume that is in the range, that is, is an element of. Let be the order of, that is, is the smallest positive integer such that. Then the numbers reduced modulo form a subgroup of whose order is and therefore, by Lagrange's theorem, divides the order of, which is. So for some positive integer and then
To prove that every element of is invertible, we may proceed as follows. First, is coprime to. Thus Bézout's identity assures us that there are integers and such that. Reading this equality modulo, we see that is an inverse for, since. Therefore, every element of is invertible. So, as remarked earlier, is a group.
For example, when, the inverses of each element are given as follows:

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 be the set whose elements are the numbers reduced modulo . If, then and therefore divides. Otherwise, there is some.
Let be the set whose elements are the numbers reduced modulo . Then has distinct elements, because otherwise there would be two distinct numbers such that, which is impossible, since it would follow that. On the other hand, no element of can be an element of, because otherwise there would be numbers such that, and then, which is impossible, since.
So, the set has elements. If it turns out to be equal to G, then and therefore divides. Otherwise, there is some and we can start all over again, defining as the set whose elements are the numbers reduced modulo . Since is finite, this process must stop at some point and this proves that divides.
For instance, if and, then, since
we have and. Clearly,. Let be an element of ; for instance, take. Then, since
we have. Clearly,. Let be an element of ; for instance, take. Then, since
we have. And now.
Note that the sets,, and so on are in fact the cosets of in.