Fermat's Little Theorem

Fermat's Little Theorem says:

ap − a is divisible by p

An example will help:

Example: a=2, p=5

ap − a = 25 − 2 = 32 − 2 = 30

And 30 is divisible by 5

Maybe that was just luck, let's try another:

Example: a=3, p=7

ap − a = 37 − 3 = 2187 − 3 = 2184

And 2184 is divisible by 7 (because 7 × 312 = 2184)

Colored Beads

necklace

It seems like a miracle that it works, but we can explain using colored beads!

We are aiming for this:

ap − a   is divisible by p

Imagine we have a=7 different types of beads 0123456 and make strings of p=5 beads like 23436 (duplicates allowed):

(7 choices for 1st bead) × (7 choices for 2nd bead) × ... × (7 choices for 5th bead) =

ap = 75  different strings

Using the Combinations and Permutations Calculator there are 16,807 strings:

00000
00001
00002
00003
. . .
14346
14350
14351
. . .
66664
66665
66666

Then we remove the a=7 "solid color" strings:

00000 X
11111 X
22222 X
33333 X
44444 X
55555 X
66666 X

And we are left with:

ap − a   =   75 − 7  strings

Now we make necklaces of them, and find we can group them like this:

00001
00010
00100
01000
10000
necklace 1

Do you see they all make the
same necklace, just rotated?

Note that we always go clockwise. More examples:

00123
01230
12300
23001
30012
necklace 123
 
00304
03040
30400
04003
40030
necklace 304

Each necklace group has p=5 strings in it, no more, no less.

The only exceptions to this rule are solid color strings, which are gone already, and strings made of a repeating pattern, which can't occur when p is prime.

All the strings can be formed into groups of 5 and so must be divisible by 5.

ap − a   =   75 − 7   is divisible by p=5

And so:

ap − a   is divisible by p

Other Forms

We can restate the formula like this (the symbol ≡ means congruent to) :

Start with:ap − a is divisible by p So for some integer k:ap − a = kp Rearrange:ap = kp + a So ap = multiples of p, with a remainder of a:ap ≡ a (mod p) Dividing both sides by a:a(p-1) ≡ 1 (mod p)

So these 3 forms are the same:

ap − a is divisible by p
ap ≡ a (mod p)
a(p-1) ≡ 1 (mod p)

Let's see them in action!

a p     ap ap-a ap-1 (ap-a)/p ap mod p ap-1 mod p
= integer = a mod p = 1
0 5 0 0 0 0 0 0
1 5 1 0 1 0 1 1
2 5 32 30 16 6 2 1
3 5 243 240 81 48 3 1
4 5 1024 1020 256 204 4 1
5 5 3125 3120 625 624 0 0
6 5 7776 7770 1296 1554 1 1

They work great, except the last form doesn't work when a is a multiple of p.

Uses

Fermat's Little Theorem is very useful in cryptography where we do crazy things like raise huge numbers to huge powers with a huge modulus.

Let's see an example using a moderately large exponent:

Example: Find the remainder when we divide 740,000 by 67

Just calculating 71,000 is hard for a spreadsheet, but all these calculations are fine:

Start with:a(p-1) ≡ 1 (mod p) a = 7, p = 67:7(67-1) ≡ 1 (mod 67) Simplify:766 ≡ 1 (mod 67) Now for some clever calcs! A power that gets us close to 40,000:40000/66 = 606 as a whole number Use that power:(766)606 ≡ 1606 (mod 67) That gets us close:739996 ≡ 1 (mod 67) Just 4 more:74(739996) ≡ 74 (mod 67) Simplify:740000 ≡ 2401 (mod 67) 2401 mod 67 = 56:740000 ≡ 56 (mod 67)

Answer: 56

Despite the many steps this is a much quicker way to find that remainder.

One more example:

Example: Find the remainder when we divide 33,000 by 19

Start with:a(p-1) ≡ 1 (mod p) a = 3, p = 19:3(19-1) ≡ 1 (mod 19) Simplify:318 ≡ 1 (mod 19)
A power to get us close to 3000:3000/18 = 166 as a whole number Use that power:(318)166 ≡ 1166 (mod 19) That gets us close:32988 ≡ 1 (mod 19) Need 12 more:312(32988) ≡ 312 (mod 19) Simplify:33000 ≡ 312 (mod 19) A few more steps to simplify 312 312 = (34)3 = 813:33000 ≡ 813 (mod 19) 81 ≡ 5 (mod 19):33000 ≡ 53 (mod 19) 53 = 125 ≡ 11 (mod 19):33000 ≡ 11 (mod 19)

Answer: 11

Why Prime?

Why does p need to be prime?

Let's try p = 6 = 2 × 3 so a string can be made of repeating patterns of 2 or 3 long such as:

necklace with repeating pattern

That example has only 3 different strings in a group:

123123
231231
312312

So we no longer have our "every necklace has p strings" rule and we become sad.

But this won't happen when p is prime. Happy again.