# Fermats Little Theorem

### Some examples:

Let's use p=7
• $2^{\small{7-1}}-1=2^6-1=64-1=63,$ a multiple of 7
• $3^{\small{7-1}}-1=3^6-1=729-1=728,$ a multiple of 7
• $4^{\small{7-1}}-1=4^6-1=4096-1=4095,$ a multiple of 7
Let's use p=5
• $2^{\small{5-1}}-1=2^4-1=16-1=15,$ a multiple of 5
• $3^{\small{5-1}}-1=3^4-1=81-1=80,$ a multiple of 5
• $4^{\small{5-1}}-1=4^4-1=256-1=255,$ a multiple of 5
Fermat's little theorem (not to be confused with Fermat's last theorem) states that if p is a prime number, then for any integer a, $a^p-a$ will be evenly divisible by p. This can be expressed in the notation of modulo arithmetic as follows:

$a^p\equiv{a}$ (mod p)

A variant of this theorem is stated in the following form:

Fermat's little theorem is the basis for the Fermat primality test.

Euler's extension of Fermat's little theorem is the basis of the RSA public-key cryptosystem.

• If n is a positive integer
• and $\phi(n)$ is how many numbers that are
• less than n and have no factors in common with n,
• then if a is one of those numbers,
• then $a^{\small\phi(n)}-1$ is a multiple of n.

## Proof

 This proof can be adapted to prove Euler's result by taking the product of all the numbers co-prime to n, rather than using the numbers from 1 to p-1.
Let p be a prime. What we're going to do is write down one product, and then evaluate it in two different ways. One way will give some number $F,$ the other will give $a^{p-1}F,$ and so these are equal.

 In the integers modulo a prime, every number from 1 to p-1 has a multiplicative inverse. We'll use that several times.
Everything is being done modulo p.

So, let's start by looking at the product $(p-1)!$ which is $F=1\text{x}{2}\text{x}\ldots\text{x}(p-1).$ This is some number which, because we are working modulo a prime, has a multiplicative inverse, $F^{-1}.$

Now let's look at the product

• $P{\equiv}(1.a)\text{x}(2a)\text{x}(3a)\text{x}\ldots\text{x}((p-1)a).$
This is the magic formula that we'll evaluate in two different ways.

 If $ma{\equiv}na,$ multiplying by $a^{-1}$ shows that $m=n.$ That means that each of the terms a, 2a, 3a, ... (p-1)a must be different.
Firstly, each of the elements here are different. The element a has a multiplicative inverse, so multiplying all the number from 1 up to p-1 can be undone. That means we can't get the same answer twice, so they're all different. That means that the product is just the same elements multiplied in a different order, and so the product is again F.

But let's rearrange it to bring all the a's to the front. Then

• $P{\equiv}(1.a)\text{x}(2a)\text{x}(3a)\text{x}\ldots\text{x}((p-1)a)\quad\quad$ (equation 1)
is the same as
• $P{\equiv}a^{p-1}(1)\text{x}(2)\text{x}(3)\text{x}\ldots\text{x}(p-1)\quad\quad$ (equation 2)
Equation (1) shows that $P{\equiv}F$ (because it's the same product in a different order)

Equation (2) shows that $P{\equiv}a^{p-1}F$

So $F{\equiv}a^{p-1}F$

Now we can multiply by $F^{-1}$ and we can see that $1{\equiv}a^{p-1}$ which is the result we wanted!