# 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* (see modular arithmetic).

## Simplifications[edit]

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 modulo p, as one can check.

Secondly, it suffices to prove that

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,

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

## Combinatorial proofs[edit]

### Proof by counting necklaces[edit]

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.^{[1]}

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:

*AAAAA*,*AAAAB*,*AAABA*,*AAABB*,*AABAA*,*AABAB*,*AABBA*,*AABBB*,*ABAAA*,*ABAAB*,*ABABA*,*ABABB*,*ABBAA*,*ABBAB*,*ABBBA*,*ABBBB*,*BAAAA*,*BAAAB*,*BAABA*,*BAABB*,*BABAA*,*BABAB*,*BABBA*,*BABBB*,*BBAAA*,*BBAAB*,*BBABA*,*BBABB*,*BBBAA*,*BBBAB*,*BBBBA*,*BBBBB*.

We will argue below that if we remove the strings consisting of a single symbol from the list (in our example, *AAAAA* and *BBBBB*), the remaining *a ^{p}* −

*a*strings can be arranged into groups, each group containing exactly p strings. It follows that

*a*−

^{p}*a*is divisible by p.

#### Necklaces[edit]

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:

*AAAAB*,*AAABA*,*AABAA*,*ABAAA*,*BAAAA*.

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

*AAAAB*,*AAABA*,*AABAA*,*ABAAA*,*BAAAA*,*AAABB*,*AABBA*,*ABBAA*,*BBAAA*,*BAAAB*,*AABAB*,*ABABA*,*BABAA*,*ABAAB*,*BAABA*,*AABBB*,*ABBBA*,*BBBAA*,*BBAAB*,*BAABB*,*ABABB*,*BABBA*,*ABBAB*,*BBABA*,*BABAB*,*ABBBB*,*BBBBA*,*BBBAB*,*BBABB*,*BABBB*,*AAAAA*,*BBBBB*.

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 32 − 2 is divisible by 5.

One can use the following rule to work out how many friends a given string *S* has:

- If
*S*is built up of several copies of the string*T*, and*T*cannot itself be broken down further into repeating strings, then the number of friends of*S*(including*S*itself) is equal to the*length*of*T*.

For example, suppose we start with the string *S* = *ABBABBABBABB*, which is built up of several copies of the shorter string *T* = *ABB*. If we rotate it one symbol at a time, we obtain the following 3 strings:

*ABBABBABBABB*,*BBABBABBABBA*,*BABBABBABBAB*.

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

#### Completing the proof[edit]

Using the above rule, we can complete the proof of Fermat's little theorem quite easily, as follows. Our starting pool of *a ^{ p}* 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 *a ^{ p}* −

*a*strings, and they may be arranged into groups of p strings, one group for each necklace. Therefore,

*a*−

^{ p}*a*must be divisible by p, as promised.

### Proof using dynamical systems[edit]

This proof uses some basic concepts from dynamical systems.^{[2]}

We start by considering a family of functions *T*_{n}(*x*), where *n* ≥ 2 is an integer, mapping the interval [0, 1] to itself by the formula

where {*y*} denotes the fractional part of *y*. For example, the function *T*_{3}(*x*) is illustrated below:

A number *x*_{0} is said to be a *fixed point* of a function *f*(*x*) if *f*(*x*_{0}) = *x*_{0}; in other words, if *f* leaves *x*_{0} 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 *T*_{3}(*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 *T*_{n}(*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,

In other words, *T*_{mn}(*x*) is the composition of *T*_{n}(*x*) and *T*_{m}(*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

So let us assume that 0 ≤ *x* < 1. In this case,

so *T*_{m}(*T*_{n}(*x*)) is given by

Therefore, what we really need to show is that

To do this we observe that {*nx*} = *nx* − *k*, 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 *T*_{ap}(*x*). We will assume that *a* ≥ 2. From Lemma 1, we know that it has *a*^{p} fixed points. By Lemma 2 we know that

so any fixed point of *T*_{a}(*x*) is automatically a fixed point of *T*_{ap}(*x*).

We are interested in the fixed points of *T*_{ap}(*x*) that are *not* fixed points of *T*_{a}(*x*). Let us call the set of such points *S*. There are *a*^{p} − *a* points in *S*, because by Lemma 1 again, *T*_{a}(*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 3^{2} − 3 = 6.

The main idea of the proof is now to split the set *S* up into its orbits under *T*_{a}. What this means is that we pick a point *x*_{0} in *S*, and repeatedly apply *T*_{a}(x) to it, to obtain the sequence of points

This sequence is called the orbit of *x*_{0} under *T*_{a}. By Lemma 2, this sequence can be rewritten as

Since we are assuming that *x*_{0} is a fixed point of *T*_{a p}(*x*), after *p* steps we hit *T*_{ap}(*x*_{0}) = *x*_{0}, 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 *x*_{0} is not a fixed point of *T*_{a}.

In other words, the orbit contains exactly *p* distinct points. This holds for every orbit of *S*. Therefore, the set *S*, which contains *a*^{p} − *a* points, can be broken up into orbits, each containing *p* points, so *a*^{p} − *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..."). *T*_{an} 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 *T*_{ap} can be thought of as the necklaces of length *p*, with *T*_{an} 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 *T*_{n} would only have *n* − 1 fixed points, but *T*_{ap} − *T*_{a} would still work out to *a*^{p} − *a*, as needed.)

### Multinomial proofs[edit]

#### Proofs using the binomial theorem[edit]

#### Proof 1[edit]

This proof, due to Euler,^{[3]} uses induction to prove the theorem for all integers *a* ≥ 0.

The base step, that 0^{p} ≡ 0 (mod *p*), is trivial. Next, we must show that if the theorem is true for *a* = *k*, then it is also true for *a* = *k* + 1. For this inductive step, we need the following lemma.

**Lemma**. For any integers *x* and *y* and for any prime *p*, (*x* + *y*)^{p} ≡ *x ^{p}* +

*y*(mod

^{p}*p*).

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

**Proof**. Assume *k*^{p} ≡ *k* (mod *p*), and consider (*k*+1)^{p}. By the lemma we have

Using the induction hypothesis, we have that *k*^{p} ≡ *k* (mod *p*); and, trivially, 1^{p} = 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* (relying on the primality 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 2[edit]

Using the Lemma, we have:

- .

#### Proof using the multinomial expansion[edit]

The proof, which was first discovered by Leibniz (who did not publish it)^{[4]} and later rediscovered by Euler,^{[3]} is a very simple application of the multinomial theorem, which states

where

and the summation is taken over all sequences of nonnegative integer indices *k*_{1}, *k*_{2}, ..., *k _{m}* such the sum of all

*k*is

_{i}*n*.

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

Clearly, if *p* is prime, and if *k _{j}* is not equal to

*p*for any

*j*, we have

and if *k _{j}* is equal to

*p*for some

*j*then

Since there are exactly *a* elements such that *k _{j}* =

*p*for some

*j*, 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 *p* can be seen as a consequence of the fact that each nontrivial necklace of length *p* can be unwrapped into a string in *p* many ways.

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

### Proof using power product expansions[edit]

An additive-combinatorial proof based on formal power product expansions was given by Giedrius Alkauskas.^{[5]} 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[edit]

This proof,^{[3]}^{[6]} discovered by James Ivory^{[7]} and rediscovered by Dirichlet^{[8]} requires some background in modular arithmetic.

Let us assume that *a* is positive and not divisible by *p*.
The idea is that if we write down the sequence of numbers

(A) |

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

(B) |

Therefore, if we multiply together the numbers in each sequence, the results must be identical modulo *p*:

Collecting together the *a* terms yields

Finally, we may “cancel out” the numbers 1, 2, ..., *p* − 1 from both sides of this equation, obtaining

There are two steps in the above proof that we need to justify:

- Why the elements of the sequence (
**A**), reduced modulo p, are a rearrangement of (**B**), and - Why it is valid to “cancel” in the setting of modular arithmetic.

We will prove these things below; let us first see an example of this proof in action.

### An example[edit]

If *a* = 3 and *p* = 7, 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 *a* = 3 and *p* = 7.

### The cancellation law[edit]

Let us first explain why it is valid, in certain situations, to “cancel”. The exact statement is as follows. If *u*, *x*, and *y* are integers, and *u* is not divisible by a prime number *p*, and if

(C) |

then we may “cancel” *u* to obtain

(D) |

Our use of this **cancellation law** in the above proof of Fermat's little theorem was valid because the numbers 1, 2, ..., *p* − 1 are certainly not divisible by *p* (indeed they are *smaller* than *p*).

We can prove the cancellation law easily using Euclid's lemma, which generally states that if a prime *p* divides a product *ab* (where *a* and *b* are integers), then *p* must divide *a* or *b*. Indeed, the assertion (**C**) simply means that *p* divides *ux* − *uy* = *u*(*x* − *y*). Since *p* is a prime which does not divide *u*, Euclid's lemma tells us that it must divide *x* − *y* instead; that is, (**D**) holds.

Note that the conditions under which the cancellation law holds are quite strict, and this explains why Fermat's little theorem demands that p is a prime. For example, 2×2 ≡ 2×5 (mod 6), but it is not true that 2 ≡ 5 (mod 6). However, the following generalization of the cancellation law holds: if u, x, y, and z are integers, if u and z are relatively prime, and if

then we may “cancel” *u* to obtain

This follows from a generalization of Euclid's lemma.

### The rearrangement property[edit]

Finally, we must explain why the sequence

when reduced modulo *p*, becomes a rearrangement of the sequence

To start with, none of the terms *a*, 2*a*, ..., (*p* − 1)*a* can be congruent to zero modulo *p*, since if *k* is one of the numbers 1, 2, ..., *p* − 1, then *k* is relatively prime with *p*, and so is *a*, so Euclid's lemma tells us that *ka* shares no factor with *p*. Therefore, at least we know that the numbers *a*, 2*a*, ..., (*p* − 1)*a*, when reduced modulo *p*, must be found among the numbers 1, 2, 3, ..., *p* − 1.

Furthermore, the numbers *a*, 2*a*, ..., (*p* − 1)*a* must all be *distinct* after reducing them modulo *p*, because if

where *k* and *m* are one of 1, 2, ..., *p* − 1, then the cancellation law tells us that

Since both *k* and *m* are between 1 and *p* − 1, they must be equal. Therefore, the terms *a*, 2*a*, ..., (*p* − 1)*a* when reduced modulo *p* must be distinct.
To summarise: when we reduce the *p* − 1 numbers *a*, 2*a*, ..., (*p* − 1)*a* modulo *p*, we obtain distinct members of the sequence 1, 2, ..., *p* − 1. Since there are exactly *p* − 1 of these, the only possibility is that the former are a rearrangement of the latter.

### Applications to Euler's theorem[edit]

This method can also be used to prove Euler's theorem, with a slight alteration in that the numbers from 1 to *p* − 1 are substituted by the numbers less than and coprime with some number m (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 *m* = 10, then the numbers less than *m* and coprime with *m* are 1, 3, 7, and 9. Thus we have:

Therefore,

## Proof as a corollary of Euler's criterion[edit]

## Proofs using group theory[edit]

### Standard proof[edit]

This proof^{[9]} requires the most basic elements of group theory.

The idea is to recognise that the set *G* = {1, 2, ..., *p* − 1}, 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 *G* is invertible. Taking this on faith for the moment, let us assume that *a* is in the range 1 ≤ *a* ≤ *p* − 1, that is, *a* is an element of *G*. Let *k* be the order of *a*, that is, *k* is the smallest positive integer such that *a ^{k}* ≡ 1 (mod

*p*). Then the numbers 1,

*a*,

*a*

^{2}, ...,

*a*

^{k −1}reduced modulo

*p*form a subgroup of

*G*whose order is

*k*and therefore, by Lagrange's theorem,

*k*divides the order of

*G*, which is

*p*− 1. So

*p*− 1 =

*km*for some positive integer

*m*and then

To prove that every element *b* of *G* is invertible, we may proceed as follows. First, *b* is coprime to *p*. Thus Bézout's identity assures us that there are integers *x* and *y* such that *bx* + *py* = 1. Reading this equality modulo *p*, we see that *x* is an inverse for *b*, since *bx* ≡ 1 (mod *p*). Therefore, every element of *G* is invertible. So, as remarked earlier, *G* is a group.

For example, when *p* = 11, the inverses of each element are given as follows:

*a*1 2 3 4 5 6 7 8 9 10 *a*^{ −1}1 6 4 3 9 2 8 7 5 10

### Euler's proof[edit]

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.^{[10]}^{[11]} Let *A* be the set whose elements are the numbers 1, *a*, *a*^{2}, ..., *a*^{k − 1} reduced modulo *p*. If *A* = *G*, then *k* = *p* − 1 and therefore *k* divides *p* −1. Otherwise, there is some *b*_{1} ∈ *G*\*A*.

Let *A*_{1} be the set whose elements are the numbers *b*_{1}, *ab*_{1}, *a*^{2}*b*_{1}, ..., *a*^{k − 1}*b*_{1} reduced modulo *p*. Then *A*_{1} has *k* distinct elements because otherwise there would be two distinct numbers *m*, *n* ∈ {0, 1, ..., *k* − 1} such that *a ^{m}b*

_{1}≡

*a*

^{n}b_{1}(mod

*p*), which is impossible, since it would follow that

*a*≡

^{m}*a*(mod

^{n}*p*). On the other hand, no element of

*A*

_{1}can be an element of

*A*, because otherwise there would be numbers

*m*,

*n*∈ {0, 1, ...,

*k*− 1} such that

*a*

^{m}b_{1}≡

*a*(mod

^{n}*p*), and then

*b*

_{1}≡

*a*

^{n}a^{k − m}≡

*a*

^{n + k − m}(mod

*p*), which is impossible, since

*b*

_{1}∉

*A*.

So, the set *A*∪*A*_{1} has 2*k* elements. If it turns out to be equal to *G*, then 2*k* = *p* −1 and therefore *k* divides *p* −1. Otherwise, there is some *b*_{2} ∈ *G*\(*A*∪*A*_{1}) and we can start all over again, defining *A*_{2} as the set whose elements are the numbers *b*_{2}, *ab*_{2}, *a*^{2}*b*_{2}, ..., *a*^{k − 1}*b*_{2} reduced modulo *p*. Since *G* is finite, this process must stop at some point and this proves that *k* divides *p* − 1.

For instance, if *a* = 5 and *p* = 13, then, since

- 5
^{2}= 25 ≡ 12 (mod 13), - 5
^{3}= 125 ≡ 8 (mod 13), - 5
^{4}= 625 ≡ 1 (mod 13),

we have *k* = 4 and *A* = {1, 5, 8, 12}. Clearly, *A* ≠ *G* = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}. Let *b*_{1} be an element of *G*\*A*; for instance, take *b*_{1} = 2. Then, since

- 2×1 = 2,
- 2×5 = 10,
- 2×8 = 16 ≡ 3 (mod 13),
- 2×12 = 24 ≡ 11 (mod 13),

we have *A*_{1} = {2, 3, 10, 11}. Clearly, *A*∪*A*_{1} ≠ *G*. Let *b*_{2} be an element of *G*\(*A*∪*A*_{1}); for instance, take *b*_{2} = 4. Then, since

- 4×1 = 4,
- 4×5 = 20 ≡ 7 (mod 13),
- 4×8 = 32 ≡ 6 (mod 13),
- 4×12 = 48 ≡ 9 (mod 13),

we have *A*_{2} = {4, 6, 7, 9}. And now *G* = *A*∪*A*_{1}∪*A*_{2}.

Note that the sets *A*, *A*_{1}, and so on are in fact the cosets of *A* in *G*.

## Notes[edit]

**^**Golomb, Solomon W. (1956), "Combinatorial proof of Fermat's "Little" Theorem" (PDF),*American Mathematical Monthly*,**63**(10): 718, doi:10.2307/2309563, JSTOR 2309563**^**Iga, Kevin (2003), "A Dynamical Systems Proof of Fermat's Little Theorem",*Mathematics Magazine*,**76**(1): 48–51, doi:10.2307/3219132, JSTOR 3219132- ^
^{a}^{b}^{c}Dickson, Leonard Eugene (2005) [1919], "Fermat's and Wilson's theorems, generalizations, and converses; symmetric funcions of 1, 2, ...,*p*− 1 modulo*p*",*History of the Theory of Numbers*, vol. I, Dover, ISBN 978-0-486-44232-7, Zbl 1214.11001 **^**Vacca, Giovanni (1894), "Intorno alla prima dimostrazione di un teorema di Fermat",*Bibliotheca Mathematica*, 2nd series (in Italian),**8**(2): 46–48**^**Alkauskas, Giedrius (2009), "A Curious Proof of Fermat's Little Theorem",*American Mathematical Monthly*,**116**(4): 362–364, arXiv:0801.0805, doi:10.4169/193009709x470236, JSTOR 40391097**^**Hardy, G. H.; Wright, E. M. (2008), "Fermat's Theorem and its Consequences",*An Introduction to the Theory of Numbers*(6th ed.), Oxford University Press, ISBN 978-0-19-921986-5**^**Ivory, James (1806), "Demonstration of a theorem respecting prime numbers",*The Mathematical Repository*, New Series,**1**(II): 6–8**^**Lejeune Dirichlet, Peter Gustav (1828), "Démonstrations nouvelles de quelques théorèmes relatifs aux nombres",*Journal für die reine und angewandte Mathematik*(in French),**3**: 390–393**^**Weil, André; Rosenlicht, Maxwell (1979), "§ VIII",*Number Theory for beginners*, Springer-Verlag, doi:10.1007/978-1-4612-9957-8, ISBN 978-0-387-90381-1, Zbl 0405.10001**^**Weil, André (2007) [1984], "§ III.VI",*Number theory: An approach through history; from Hammurapi to Legendre*, Birkhäuser, ISBN 978-0-8176-4565-6, Zbl 1149.01013**^**Euler, Leonhard (1761), "Theoremata circa residua ex divisione potestatum relicta" (PDF),*Novi Commentarii Academiae Scientiarum Petropolitanae*(in Latin),**7**: 49–82