This lecture will focus on Wilson’s Theorem. Let \(m\) be an integer. Consider the following

$$ \begin{align*} (m-1)! \pmod{m} \end{align*} $$

Trying different values for \(m\), we see the following pattern

\(m\) \(m - 1\) \((m - 1)! \bmod m\)
\(1\)\(0\)\(1\)
\(2\)\(1\)\(-1\)
\(3\)\(2\)\(-1\)
\(4\)\(6\)\(2\)
\(5\)\(24\)\(-1\)
\(6\)\(120\)\(0\)
\(7\)\(720\)\(-1\)
\(8\)\(5040\)\(0\)
\(9\)\(40320\)\(0\)

Observe here that \((m-1)! \bmod m\) is \(-1\) when \(m\) is prime. This is precisely Wilson’s Theorem. We state it again as follow

If \(p\) is prime, then $$ \begin{align*} (p-1)! \equiv -1 \pmod{p} \end{align*} $$

Next, we will see an example to informally show why this theorem works.


Informal Proof

Let’s start with finding \((11-1)! \pmod{11}\). We can expand \((11-1)!\) to see that

$$ \begin{align*} 10 \cdot 9 \cdot 8 \cdot 7 \cdot 6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1 \pmod{11} \end{align*} $$

We can pair the numbers together to see that

$$ \begin{align*} 2 \cdot 6 \equiv 12 \equiv 1 \pmod{11} \\ 3 \cdot 4 \equiv 12 \equiv 1 \pmod{11} \\ 5 \cdot 9 \equiv 45 \equiv 1 \pmod{11} \\ 7 \cdot 8 \equiv 56 \equiv 1 \pmod{11} \end{align*} $$

Therefore

$$ \begin{align*} &\equiv 1 \cdot (2 \cdot 6) \cdot (3 \cdot 4) \cdot (5 \cdot 9) \cdot (7 \cdot 8) \cdot 10 \pmod{11} \\ &\equiv 1 \cdot 10 \pmod{11} \\ &\equiv 10 \pmod{11} \\ &\equiv -1 \pmod{11} \end{align*} $$

Also note here that \(1\) is its own inverse module \(11\). Similarly, \(10\) is its own inverse module \(11\). This is the reason they were the ones left over at the end. In general, given \(a\), if \(a \neq a^{-1}\), we see that they cancel. When \(a^{-1} = a\), they don’t cancel. Moreover, we see that in this case

$$ \begin{align*} a &\equiv a^{-1} \pmod{11} \\ a^2 &\equiv 1 \pmod{11} \\ a^2 - 1&\equiv 0 \pmod{11} \\ (a-1)(a+1) &\equiv 0 \pmod{11} \end{align*} $$

But \(11\) is prime so either \(a - 1 \equiv 0 \pmod{11}\) or \(a + 1\equiv 0 \pmod{11}\) (Reminder: this is because \(ab \equiv 0 \pmod {p}\) means that \(p\) divides \(ab\) but \(p\) is prime so it must divide \(a\) or \(b\) or both). Therefore,

$$ \begin{align*} a \equiv \pm 1 \pmod{11} \end{align*} $$

We can now generalize this for any prime \(p\). The expansion of \((p-1)!\) is

$$ \begin{align*} 1, 2, \cdots, (p-2)(p-1) \end{align*} $$

All the numbers in the middle from \(2\) to \(p-2\) will pair off. What remains is

$$ \begin{align*} (p-1)! &\equiv 1 \cdot (p-1) \pmod{p} \\ &\equiv (p-1) \pmod{p} \\ &\equiv -1 \pmod{p} \\ \end{align*} $$

This is great because now we have another test for primality as follows

\(m > 1\) is prime if and only if \((m-1) \equiv -1 \pmod{m}\)

But this test is totally useless! since it’s hard to figure out \((m-1)! \bmod m\) without knowing whether \(m\) itself is prime to be able to use Wilson’s theorem. In general, it is hard to compute \(a! \bmod m\)


When \(m\) is not Prime

What if \(m\) is not prime? From the table above, can we conclude that

$$ \begin{align*} (m-1)! \equiv 0 \pmod{m}? \end{align*} $$

Since \(m\) is not prime, then we can write

$$ \begin{align*} m = ab \quad \text{ for } 1 < a,b < m \end{align*} $$

\(a\) and \(b\) are strictly less than \(m\). This means that they must show up in the expansion of \((m-1)!\). But this means that

$$ \begin{align*} ab \mid (m-1)! \end{align*} $$

which makes the claim correct. However this is not true in general. Take \(m = 4\). Then \(4 = 2 \cdot 2\). The issue here that the two factors are exactly the same so the argument above doesn’t work. Exercise: work out the argument for when \(m\) is composite and \(m \neq 4\). This should work even though some numbers do have square factors.


Application 1: Square Root of \(-1\)

Find \(\sqrt{-1}\).

Over \(\mathbb{R}\), we don’t have a solution. Over \(\mathbb{C}\), there is a solution but we’re not interested in that. We’re interested in a solution over \(\mathbb{Z}_p\). So does \(-1\) have a square root module \(p\) when \(p\) is prime? This statement is equivalent to solving

$$ \begin{align*} x^2 \equiv -1 \pmod{p} \end{align*} $$

Square both sides to see that

$$ \begin{align*} x^2 &\equiv -1 \pmod{p} \\ x^4 &\equiv 1 \pmod{p} \end{align*} $$

so \(x\) has order \(4\). But by Fermat’s Theorem and since \(p\) is prime, we know that

$$ \begin{align*} x^{p-1} &\equiv 1 \pmod{p} \end{align*} $$

We also know from the last lecture that the order of \(x\) must divide \(p - 1\). So this means that we must have \(4 \mid p - 1\). We can then write this as

$$ \begin{align*} p &\equiv 1 \pmod{4} \\ \end{align*} $$

So to have a solution \(p\) must be congruent to \(1\) module \(4\). So now the problem can be stated as

If \(p \equiv 1 \pmod{4}\), Does \(-1\) have a square root module \(p\)?

Checking a few cases. Take \(p = 5, 13, 17 \cdots\). We will see that

$$ \begin{align*} 2^2 &\equiv -1 \pmod{5}\\ 5^2 &\equiv 12 \equiv -1 \pmod{13}\\ 4^2 &\equiv 16 \equiv -1 \pmod{17} \end{align*} $$

Things seem to work. Can we prove that this work in general? yes, It turns out that

If \(p \equiv 1 \bmod 4\), then \(-1\) has a square root module \(p\). Furthermore, the square root of \(-1\) is as follows $$ \begin{align*} \left[\left(\frac{p-1}{2}\right)!\right]^2 \equiv -1 \pmod{p} \\ \left(\frac{p-1}{2}\right)! \equiv \sqrt{-1} \pmod{p} \end{align*} $$

Why? Take \(p = 13\). The expansion of \((13-1)!\) is

$$ \begin{align*} 1 \cdot 2 \cdot 3 \cdot 4 \cdot 6 \cdot 7 \cdot 8 \cdot 9 \cdot 10 \cdot 11 \cdot 12 \end{align*} $$

Now, \(\left(\frac{p-1}{2}\right)!\) is the product of the first \(6\) elements above. But these \(6\) elements pair off with the remaining \(6\) numbers (except for the sign) meaning that

$$ \begin{align*} 7 \equiv -6 \pmod{13} \\ 8 \equiv -5 \pmod{13} \\ 9 \equiv -4 \pmod{13} \\ 10 \equiv -3 \pmod{13} \\ 11 \equiv -2 \pmod{13} \\ 12 \equiv -1 \pmod{13} \\ \end{align*} $$

Therefore, we can write

$$ \begin{align*} (p-1)! \equiv \left(\frac{p-1}{2}\right)! \cdot \left(\frac{p-1}{2}\right)! \cdot (-1)^{\frac{p-1}{2}} \pmod{p} \end{align*} $$

But by Wilson’s theorem, we know that

$$ \begin{align*} (p-1)! \equiv -1 \pmod{p} \end{align*} $$

Thus

$$ \begin{align*} -1 &\equiv \left(\frac{p-1}{2}\right)! \cdot \left(\frac{p-1}{2}\right)! \cdot (-1)^{\frac{p-1}{2}} \pmod{p} \end{align*} $$

In other words,

$$ \begin{align*} -1 &\equiv \left[\left(\frac{p-1}{2}\right)!\right]^2 \cdot (-1)^{\frac{p-1}{2}} \pmod{p} \end{align*} $$

Recall that

$$ \begin{align*} p \equiv 1 \pmod{4} \end{align*} $$

so \(p - 1 = 4k\) for some \(k\). Therefore

$$ \begin{align*} \frac{p-1}{2} = \frac{4k}{2} = 2k \end{align*} $$

So \(\frac{p-1}{2}\) is even. Therefore In other words,

$$ \begin{align*} -1 &\equiv \left[\left(\frac{p-1}{2}\right)!\right]^2 \cdot (-1)^{\frac{p-1}{2}} \pmod{p} \\ -1 &\equiv \left[\left(\frac{p-1}{2}\right)!\right]^2 \pmod{p} \\ \end{align*} $$

as we wanted to show. \(\blacksquare\)

So now we might ask

If \(p \equiv 3 \pmod{4}\), Does \(-1\) have a square root module \(p\)?

Again, we have the same equation

$$ \begin{align*} -1 &\equiv \left[\left(\frac{p-1}{2}\right)!\right]^2 \cdot (-1)^{\frac{p-1}{2}}\pmod{p} \end{align*} $$

But we can see here that \(p = 4k + 3\). Therefore,

$$ \begin{align*} \frac{p-1}{2} = \frac{4k+3-1}{2} = \frac{4k+2}{2} = 2k+1 \end{align*} $$

Thus, \(\frac{p-1}{2}\) is now odd and not even. Therefore

$$ \begin{align*} 1 &\equiv \left[\left(\frac{p-1}{2}\right)!\right]^2 \pmod{p} \end{align*} $$

But this means that we have two solutions so

$$ \begin{align*} \pm 1 &\equiv \left(\frac{p-1}{2}\right)! \pmod{p} \end{align*} $$

Both cases can occur. For example when \(p = 3\), then this a square root of \(-1\) while if \(p = 7\), then this is a square root of \(1\).


Application 2: Proof of Fermat / Euler

We can use the same idea to try to prove Fermat/Euler’s theorems.

$$ \begin{align*} \text{Fermat:} \quad \quad a^{p-1} &\equiv 1 \pmod{p} \\ \text{Euler:} \quad \quad a^{\phi(m)} &\equiv 1 \pmod{m} \\ \end{align*} $$

Proof: consider the product

$$ \begin{align*} P = 1 \cdot 2 \cdots (p-1) \pmod{p} \end{align*} $$

and then compare this against

$$ \begin{align*} Q &= a \cdot (2a) \cdot (3a) \cdots (p-1)a \pmod{p} \quad \text{ where } (a,p)=1 \\ \end{align*} $$

Since we’re working module \(p\), then the numbers above are exactly the same as the first product but just in a different order. So

$$ \begin{align*} P &\equiv Q \pmod{p} \\ 1 \cdot 2 \cdots (p-1) \pmod{p} &\equiv a \cdot (2a) \cdot (3a) \cdots (p-1)a \pmod{p} \\ \end{align*} $$

But now observe that we can group these factors in a way to get

$$ \begin{align*} 1 \cdot 2 \cdots (p-1) \pmod{p} &\equiv a \cdot (2a) \cdot (3a) \cdots (p-1)a \pmod{p} \\ (p-1)! \pmod{p} &\equiv (1 \cdot 2 \cdot 3 \cdot 4 \cdots (p-1)) \cdot a^{p-1} \pmod{p} \\ (p-1)! &\equiv (p-1)! \cdot a^{p-1} \pmod{p} \end{align*} $$

Recall that Wilson’s theorem that states that

$$ \begin{align*} (p-1)! \equiv -1 \pmod{p} \end{align*} $$

Therefore

$$ \begin{align*} (p-1)! &\equiv (p-1)! \cdot a^{p-1} \pmod{p} \\ -1 &\equiv -1 \cdot a^{p-1} \pmod{p} \\ 1 &\equiv a^{p-1} \pmod{p} \end{align*} $$

And so we proved Fermat’s theorem. We can use the same technique to prove Euler’s theorem. It’s the same proof except but we want to multiply all numbers coprime to \(m\).


Application 3

Suppose now that \(m\) is not prime.

What is the product of all residue classes coprime to \(m\)?

We always want to look at examples first.

\(m\) Coprime numbers to \(m\) \(\prod \bmod m\)
\(1\)\(1\)\(1\)
\(2\)\(1\)\(-1\)
\(3\)\(1,2\)\(-1\)
\(4\)\(1,3\)\(-1\)
\(5\)\(1,2,3,4\)\(-1\)
\(6\)\(1,5\)\(-1\)
\(7\)\(1,2,3,4,5,6\)\(-1\)
\(8\)\(1,3,5,7\)\(1\)
\(9\)\(1,2,4,5,7,8\)\(-1\)

So for some reason the product module \(m\) when \(m = 8\), is \(1\) and not \(-1\). So what is the product of all residue classes coprime to \(m\) module \(m\)? We can use the same trick of pairing out elements. Recall that if \(a \neq a^{-1}\), then the product is \(a^{-1}a \equiv 1 \pmod{m}\) so these elements will cancel out. Then, we’re left with all the elements such that \(a = a^{-1}\). In this case, these elements don’t cancel out with anything. So now the product looks like

$$ \begin{align*} \prod_{a = a^{-1}} a \pmod{m} \end{align*} $$

Suppose now that there is only one number such that \(a \neq 1\) and \(a = a^{-1}\). In this case, \(a\) must be \(-1\) since \(a^2=(-1)^2 = 1\). Then, in this case we get Wilson’s theorem.

$$ \begin{align*} \prod_{(a,m) = 1} a \equiv -1 \pmod{m} \end{align*} $$

What if there are other numbers? So suppose that there is one more number \(a\) such that \(a \neq \pm 1\) and \(a^2 \equiv 1 \bmod m\). Then, we have the following

$$ \begin{align*} a^2 &\equiv 1 \pmod{m} \\ (-a)^2 &\equiv 1 \pmod{m} \end{align*} $$

Furthermore, if we re-consider \(-1, 1\), then we have 4 numbers \(\{1, -1, a, -a\}\) such that \(x^2 \equiv 1 \pmod{m}\). . In this case, the product

$$ \begin{align*} 1 \cdot -1 \cdot a \cdot -a \equiv 1 \pmod{m} \end{align*} $$

So for \(m = 8\)

$$ \begin{align*} 1 \cdot -1 \cdot 3 \cdot -3 \equiv 1 \pmod{8} \end{align*} $$

Now, what if there are even more numbers than just 4 numbers? So we have \(a \neq \pm 1\) such that \(a^2 \equiv 1\). Suppose now, we also have \(b^2 \equiv 1\). But then we get these additional numbers that are congruent to \(1\) (This is in addition to the numbers we found earlier \(\{-a,a,-1,1\}\). The product of the new numbers is also congruent to 1.

$$ \begin{align*} b \cdot -b \cdot ab \cdot -ab \equiv 1 \pmod{m} \end{align*} $$

So whenever we add a new number such that \(c^2 \equiv 1\), we get 4 new numbers. This will make the product congruent to 1 every time. Therefore,

$$ \begin{align*} \prod_{a^2 \equiv 1} \equiv 1 \pmod{m} \end{align*} $$

Thus

$$ \begin{align*} \prod_{(a,m)} \equiv \pm 1 \pmod{m} \end{align*} $$

It is \(-1\) if \(-1\) is the only solution to \(x^2 \equiv 1\) and it is 1 if there are more solution to \(x^2 \equiv\) other than \(-1\).


References