# Lecture 17

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 $0,3,6,9,12,15,18.$ Regarded modulo 7, each is congruent to something different: \begin{aligned} {0}&{\equiv 0} & {18}&{\equiv 4} \\ {15}&{\equiv 1} & {12}&{\equiv 5} \\ {9}&{\equiv 2} & {6}&{\equiv 6} \\ {3}&{\equiv 3} &&\end{aligned} Can we explain this systematically?

It comes down to the fact that $3$ is invertible modulo $7$ (with inverse $5$, as $3\times 5\equiv{1}\pmod{7}$).

By multiplying congruences, $3\times 5\times a\equiv a\pmod{7}$ so if we want to solve $3x\equiv a\pmod{7}$, we simply take $x\equiv 5a\pmod{7}$.

So as there are seven numbers in the list, and one is congruent to each possible residue $0,1,\ldots,6$ (modulo 7), they’re all different.

This is true in general, for the same reason: if $a$ is coprime to $m$, then the integers $0,a,2a,\ldots,(m-1)a$ contain each of the $m$ residues (and so exactly once each, because there’s $m$ of them).

#### Proof

Consider the product $a\cdot(2a)\cdot(3a)\cdot\cdots\cdot((p-1)a),$ regarded up to congruence modulo $p$.

One way of thinking about it is that it’s $(p-1)!$ but with every term multiplied by an $a$, so is congruent to $a^{p-1}(p-1)!$.

Another is that, since the product contains a copy of every nonzero residue modulo $p$, it is congruent to $(p-1)!$.

But, putting these observations together, we discover that $a^{p-1}(p-1)! \equiv (p-1)!\pmod{p}.$ But all the residues from $1$ to $p-1$ are invertible, and the product of invertible residues is invertible, so $(p-1)!$ is invertible. Multiplying both sides by $(p-1)!^{-1}$ leaves us with $a^{p-1}\equiv1\pmod{p},$ 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 $a^n+b^n=c^n$ with $n\geq 3$, 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) $\varphi:\mathbb{N}\rightarrow\mathbb{N}$ is defined by taking $\varphi(n)$ to be the number of integers from $1$ to $n$ which are coprime to $n$.

For example, $\varphi(p) = p-1$ if $p$ is prime, since every number from $1$ to $p-1$ is coprime to $p$ (and $p$ isn’t coprime to $p$).

For another example, $\varphi(6) = 2$, since $1$ and $5$ are the only numbers between $1$ and $6$ which are coprime to $6$.

Using this concept, we can generalise Fermat’s Little Theorem considerably:

#### Proof

The proof is exactly the same as Fermat’s Little Theorem, but instead of working with all the integers $1,2,\ldots,n-1$, we just consider those that are invertible modulo $n$: let’s write these as $x_1,x_2,\ldots,x_{\varphi(n)}$.

If $a$ is invertible, then $ax_1,\ldots,ax_{\varphi(n)}$ are all invertible too, and any invertible residue is of this form: $b$ can be written as $a(a^{-1}b)$. Hence $ax_1,ax_2,\ldots,ax_{\varphi(n)}$ are congruent to $x_1,x_2,\ldots,x_{\varphi(n)}$ in some order.

Hence if we consider the products of these we have \begin{aligned} & x_1x_2\cdots x_{\varphi(n)}\\ \equiv& (ax_1)(ax_2)\cdots(ax_{\varphi(n)})\\ \equiv& a^{\varphi(n)}x_1x_2\cdots x_{\varphi(n)}\pmod{n}\end{aligned} Since all the elements $x_1,x_2,\ldots,x_{\varphi(n)}$ are invertible, we can cancel them out to get $a^{\varphi(n)}\equiv 1\pmod{n}$.

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:

#### Proposition

Let $p$ be a prime, and let $a$ be an integer with the property that $a^2\equiv 1\pmod{p}$. Then either $a\equiv1\pmod{p}$ or $a\equiv-1\pmod{p}$.

#### Proof

If $a^2\equiv 1\pmod{p}$, then $a^2-1\equiv 0\pmod{p}$, ie $(a-1)(a+1)\equiv 0\pmod{p}$. In other words, $p\mid(a-1)(a+1)$.

But then, either $p\mid a-1$ (in which case $a\equiv1\pmod{p}$), or $p\mid a+1$ (in which case $a\equiv-1\pmod{p}$).

This theorem is not true for some composite moduli! For example, $1^2\equiv 3^2\equiv 5^2\equiv 7^2\equiv 1\pmod{8}$.

I regard this as more evidence that prime moduli behave very nicely indeed!

This means that if we have $a$ not congruent to $\pm 1$ modulo a prime $p$, then the inverse of $a$ (modulo $p$) is different to $a$.

Indeed, if $a\equiv a^{-1}$ then $1\equiv aa^{-1}\equiv a^2$.

Now, this allows us to do this:

#### Proof

I’ll show firstly that if $n$ is composite, we don’t get $(n-1)!\equiv-1\pmod{n}$.

Indeed, suppose that $n$ has a factor $a$ such that $1. Then we certainly have $a\mid(n-1)!$, and so $(n-1)!\equiv 0\pmod{a}$. However, if $(n-1)!\equiv -1\pmod{n}$ and $a|n$, then $(n-1)!\equiv -1\pmod{a}$, which gives a contradiction.

Now I’ll show that if $n$ is prime we do get $(n-1)!\equiv-1\pmod{n}$.

Given that $n$ is prime, the product $(n-1)! = 1\cdot 2\cdot\cdots\cdot(n-1)$ 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 $1$ and $-1$. So, the product consists of a lot of pairs of inverses (whose product modulo $n$ is $1$), together with the odd ones out $1$ and $-1$: so the product is $-1$ as claimed.

Here are some examples:

• $9! = 362880 \equiv 0 \pmod{10}$, and so $10$ is composite.

• $10! = 3628800 \equiv -1 \pmod{11}$, and so $11$ is prime. Indeed, $2$ and $6$ are inverses, and $3$ and $4$, and $5$ and $9$, and $7$ and $8$.

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 $N$, it’s quicker to do trial division to see if $N$ 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 $N$ is prime or not by looking at how arithmetic modulo $N$ behaves.