It turns out that that we can get an explicit result. First we’ll do a relatively easy case, valid when the modulus is prime.
Before we prove it, we’ll talk a while longer about invertible elements and multiplication modulo a prime.
Let’s start with an example, and consider the seven integers Regarded modulo 7, each is congruent to something different: Can we explain this systematically?
It comes down to the fact that is invertible modulo (with inverse , as ).
By multiplying congruences, so if we want to solve , we simply take .
So as there are seven numbers in the list, and one is congruent to each possible residue (modulo 7), they’re all different.
This is true in general, for the same reason: if is coprime to , then the integers contain each of the residues (and so exactly once each, because there’s of them).
Consider the product regarded up to congruence modulo .
One way of thinking about it is that it’s but with every term multiplied by an , so is congruent to .
Another is that, since the product contains a copy of every nonzero residue modulo , it is congruent to .
But, putting these observations together, we discover that But all the residues from to are invertible, and the product of invertible residues is invertible, so is invertible. Multiplying both sides by leaves us with exactly as promised.
Fermat’s Little Theorem should not be confused with Fermat’s Last Theorem. The latter says there are no solutions in positive integers to with , and was much, much harder to prove.
In the proof of Fermat’s Little Theorem, we multiplied one representative of each invertible residue class together. It turns out we can prove a substantially more general theorem, but it’s a little more complicated. First we need a definition:
Definition: Euler’s function (sometimes known as the totient function) is defined by taking to be the number of integers from to which are coprime to .
For example, if is prime, since every number from to is coprime to (and isn’t coprime to ).
For another example, , since and are the only numbers between and which are coprime to .
Using this concept, we can generalise Fermat’s Little Theorem considerably:
The proof is exactly the same as Fermat’s Little Theorem, but instead of working with all the integers , we just consider those that are invertible modulo : let’s write these as .
If is invertible, then are all invertible too, and any invertible residue is of this form: can be written as . Hence are congruent to in some order.
Hence if we consider the products of these we have Since all the elements are invertible, we can cancel them out to get .
We worked with the factorial in the proof of Fermat’s Little Theorem without ever needing to calculate it. It turns out we can calculate it, using a clever trick.
However, we’ll need a fact first:
Let be a prime, and let be an integer with the property that . Then either or .
If , then , ie . In other words, .
But then, either (in which case ), or (in which case ).
This theorem is not true for some composite moduli! For example, .
I regard this as more evidence that prime moduli behave very nicely indeed!
This means that if we have not congruent to modulo a prime , then the inverse of (modulo ) is different to .
Indeed, if then .
Now, this allows us to do this:
I’ll show firstly that if is composite, we don’t get .
Indeed, suppose that has a factor such that . Then we certainly have , and so . However, if and , then , which gives a contradiction.
Now I’ll show that if is prime we do get .
Given that is prime, the product consists of one representative of each invertible residue class.
We can pair each up with its inverse; each element gets paired with another, except for and . So, the product consists of a lot of pairs of inverses (whose product modulo is ), together with the odd ones out and : so the product is as claimed.
Here are some examples:
, and so is composite.
, and so is prime. Indeed, and are inverses, and and , and and , and and .
You could use this as a way of testing if a number is prime.
As a matter of fact, it’s not a good way of doing it: if we want to check a large number , it’s quicker to do trial division to see if has any factors, than it is to multiply lots of numbers together.
But this result was psychologically important in the development of modern fast primality tests: it was the first evidence that there are ways of investigating whether a number is prime or not by looking at how arithmetic modulo behaves.