# Modular Arithmetic/Fermat's Little Theorem

 Modular Arithmetic ← Problem Solving with Modular Arithmetic Fermat's Little Theorem Euler's Theorem →
Example: Powers of 3

Let us look at the powers of a number under modulo arithmetic. We'll look at:

${\displaystyle \displaystyle 3,3^{2},3^{3},3^{4}...}$

and we're going to look modulo a prime number ${\displaystyle \displaystyle p}$. First we'll choose ${\displaystyle \displaystyle p=7}$. We could work out each number and then reduce, e.g.

${\displaystyle \displaystyle 3^{4}=3\times 3\times 3\times 3=81\equiv 4\,\mathrm {mod} \,7}$
${\displaystyle \displaystyle 3^{5}=3\times 3\times 3\times 3\times 3=243\equiv 5\,\mathrm {mod} \,7}$

but it is quicker just to work out ${\displaystyle \displaystyle 3^{n+1}}$ from ${\displaystyle \displaystyle 3^{n}}$ like so:

${\displaystyle \displaystyle 3^{1}=3\quad \quad \quad \equiv 3\,\mathrm {mod} \,7}$
${\displaystyle \displaystyle 3^{2}=3\times 3=9\equiv 2\,\mathrm {mod} \,7}$
${\displaystyle \displaystyle 3^{3}=2\times 3=6\equiv 6\,\mathrm {mod} \,7}$
${\displaystyle \displaystyle 3^{4}=6\times 3=18\equiv 4\,\mathrm {mod} \,7}$
${\displaystyle \displaystyle 3^{5}=4\times 3=12\equiv 5\,\mathrm {mod} \,7}$
${\displaystyle \displaystyle 3^{6}=5\times 3=15\equiv 1\,\mathrm {mod} \,7}$
${\displaystyle \displaystyle 3^{7}=1\times 3=3\equiv 3\,\mathrm {mod} \,7}$
${\displaystyle \displaystyle 3^{8}=3\times 3=9\equiv 2\,\mathrm {mod} \,7}$

it is quite clear that the pattern is repeating. That's because ${\displaystyle \displaystyle 3^{6}\equiv 1\,{\bmod {\,}}7}$. Even without doing the calculation it is clear that the pattern has to repeat somewhere. There are at most 7 different possible numbers for ${\displaystyle \displaystyle 3^{n}\,{\bmod {\,}}7}$ so if we calculate it for ${\displaystyle \displaystyle n=1,2,3,4,5,6,7,8}$ there has to be a repeated answer somewhere in there.

Note: This is a use of the pigeonhole principle - there are more pigeons than pigeonholes, so one pigeonhole must contain two pigeons. If you are interested, click on the pigeon icon to read more about the pigeonhole principle:

 Pigeons: Powers of 3 ${\displaystyle \displaystyle {\bmod {\,}}7}$. Pigeonholes: The seven possible remainders ${\displaystyle \displaystyle {\bmod {\,}}7}$.

Once a number is repeated the sequence from there on must be the same as before, from the first occurrence of that number. We can even see that we will get a 1 followed by a 3 as where the repeat first happens. Why?

Suppose we have some repeated number, in other words ${\displaystyle \displaystyle 3^{a}\equiv 3^{a+b}}$ with a and b positive numbers. Then ${\displaystyle \displaystyle 3^{a}\equiv 3^{a}3^{b}}$ and we can divide both sides by ${\displaystyle \displaystyle 3^{a}}$ to get ${\displaystyle \displaystyle 1\equiv 3^{b}}$, i.e. a repeat that happens sooner. Just a little care is needed. In modulo arithmetic it is not always allowed to divide by a common factor. We're allowed to do that division here because we earlier established it for modulo a prime using Euclid's algorithm. We know that ${\displaystyle \displaystyle 3^{n}\,\mathrm {mod} \,7}$ is not zero since otherwise 3 would be a factor of 7 and 7 is prime.

Something to watch out for with modular arithmetic, we cannot just reduce numbers wherever we see them. For example working ${\displaystyle \displaystyle {\bmod {\,}}7}$, the exponent of ${\displaystyle \displaystyle 8}$ in ${\displaystyle \displaystyle 3^{8}}$ cannot just be replaced by ${\displaystyle \displaystyle 1}$ because ${\displaystyle 3^{8}\neq 3^{1}\,{\bmod {7}}}$. The ones to watch are in exponents. In expressions like ${\displaystyle \displaystyle 1000x\,{\bmod {7}}}$ and ${\displaystyle \displaystyle (1000+x)\,{\bmod {7}}}$ it is fine to replace the 1000 to get ${\displaystyle \displaystyle 6x\,{\bmod {7}}}$ and ${\displaystyle \displaystyle (6+x)\,{\bmod {7}}}$ or even ${\displaystyle \displaystyle -x\,{\bmod {7}}}$ and ${\displaystyle \displaystyle (x-1)\,{\bmod {7}}}$

 Exercise: Powers of 3 Now your turn. Do exactly the same thing as in the example, but this time for ${\displaystyle \displaystyle p=11}$

There is nothing really special about 3 here. We could do exactly the same exercise for other numbers ${\displaystyle \displaystyle a}$ with ${\displaystyle \displaystyle a=1,2,4,5\,or\,6}$. We might reach ${\displaystyle \displaystyle a^{n}\equiv 1}$ sooner, we definitely will for ${\displaystyle \displaystyle a=1}$, but we would still have ${\displaystyle \displaystyle a^{(p-1)}\equiv 1\,{\pmod {p}}}$.

We're going to prove this several ways. The reason for making such a meal out of proving it is that it helps to see different ways of proving a result. In this case it's mainly a way to show the different notation that can be used. The third variant of the proof will also introduce the concept of multiplicative functions which will be important later on.