Three Theorems in Number Theory

In this post, I would like to discuss three elementary theorems in number theory that I first learned in my middle school. It has been really ages since then, and I only vaguely remember their proofs.

During a rather boring midnight coach ride to the airport, I found myself with nothing to do but share some random mathematical thoughts with my wife. As the journey went on, the proofs gradually became clearer in my mind—and, meanwhile, she happily fell asleep.

Let’s start with Fermat’s little theorem.

Theorem 1 (Fermat). Let 𝑝 be a prime number. For any 𝑎 coprime to 𝑝, we have 𝑎𝑝1=1(mod𝑝).

Proof. Let 𝑆={1,2,,𝑝1}. We notice that the set 𝑎𝑆{𝑎,2𝑎,,𝑎(𝑝1)} coincides with 𝑆, since 𝑎 and 𝑝 are coprime. We immediately get

(𝑝1)!=𝑛𝑆𝑛=𝑛𝑎𝑆𝑛=𝑎𝑝1(𝑝1)!(mod𝑝).

After canceling out (𝑝1)! on both sides of the equation, we derive 𝑎𝑝1=1(mod𝑝).

The next theorem also involves a double counting technique.

Theorem 2 (Wilson). Let 𝑝 be a prime number. We have (𝑝1)!=1(mod𝑝).

Proof. The elements in 𝑆={1,2,,𝑝1} can be grouped into pairs (𝑎,𝑎1), where 𝑎1 is the unique element such that 𝑎𝑎1=1(mod𝑝). There are only two special cases where 𝑎=𝑎1, namely 𝑎=1 or 𝑎=𝑝1. So, when we compute 𝑛𝑆𝑛, almost all the terms in the same pair cancel out and only leave 1 and 𝑝1. Therefore, we derive

(𝑝1)!=1(𝑝1)=1(mod𝑝).


The last theorem is also named after Fermat. But, as usual, he did not write down the proof. The first proof was given by Euler using infinite descent. Here, I present a more constructive proof.

Theorem 3 (Sum of two squares). A prime number 𝑝 can be represented as a sum of two squares if and only if 𝑝=1(mod4).

Proof. The necessity is obvious. Now, we assume that 𝑝=1(mod4). By Wilson’s theorem, we have (𝑝1)!=1(mod𝑝). In particular, it yields

1=(𝑝1)!=𝑛=1𝑝12𝑛(𝑝𝑛)=(1)𝑝12𝑛=1𝑝12𝑛2=(𝑛=1𝑝12𝑛)2(mod𝑝).

Therefore, there exists 𝑎 such that 𝑎2=1(mod𝑝).

Let 𝑛=𝑝 and we consider the set 𝐴={0,𝑎,,𝑛𝑎}. Since |𝐴|=𝑛+1, by pigeonhole principle, there must be two elements (𝑥𝑎,𝑦𝑎) in 𝐴 such that 𝑥𝑎𝑦𝑎=𝑧(mod𝑝) for some 0<𝑧𝑛. We notice that

0<(𝑥𝑦)2+𝑧2𝑛2+𝑛2<2𝑝.

Moreover, by our choice of 𝑎,

(𝑥𝑦)2+𝑧2=(𝑥𝑦)2+(𝑥𝑎𝑦𝑎)2=(𝑥𝑦)2(𝑎2+1)=0(mod𝑝).

We deduce that (𝑥𝑦)2+𝑧2=𝑝.