# Difference between revisions of "Modular arithmetic/Introduction"

Modular arithmetic is a special type of arithmetic that involves only integers.

## The Basic Idea

Let's use a clock as an example, except let's replace the $\displaystyle 12$ at the top of the clock with a $\displaystyle 0$. Starting at noon, the hour hand points in order to the following:

$\displaystyle 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, \ldots$

This is the way in which we count in modulo 12. When we add $\displaystyle 1$ to $\displaystyle 11$, we arrive back at $\displaystyle 0$. The same is true in any other modulus (modular arithmetic system). In modulo $\displaystyle 5$, we count

$\displaystyle 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, \ldots$

We can also count backwards in modulo 5. Any time we subtract 1 from 0, we get 4. So, the integers from $\displaystyle -12$ to $\displaystyle 0$, when written in modulo 5, are

$\displaystyle 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0,$

where $\displaystyle -12$ is the same as $\displaystyle 3$ in modulo 5. Because all integers can be expressed as $\displaystyle 0$, $\displaystyle 1$, $\displaystyle 2$, $\displaystyle 3$, or $\displaystyle 4$ in modulo 5, we give these give integers their own name: the modulo 5 residues. In general, for a natural number $\displaystyle n$ that is greater than 1, the modulo $\displaystyle n$ residues are the integers that are whole numbers less than $\displaystyle n$:

$\displaystyle 0, 1, 2, \ldots, n-1.$

This just relates each integer to its remainder from the Division Theorem. While this may not seem all that useful at first, counting in this way can help us solve an enormous array of number theory problems much more easily!

## Congruence

There is a mathematical way of saying that all of the integers are the same as one of the modulo 5 residues. For instance, we say that 7 and 2 are congruent modulo 5. We write this using the symbol $\displaystyle \equiv$:

$\displaystyle 7 \equiv 2 \pmod{5}.$

The (mod 5) part just tells us that we are working with the integers modulo 5. In modulo 5, two integers are congruent when their difference is a multiple of 5. Thus each of the following integers is congruent modulo 5:

$\displaystyle -12 \equiv -7 \equiv -2 \equiv 3 \equiv 8 \equiv 13 \equiv 18 \equiv 23 \pmod{5}$

In general, two integers $\displaystyle a$ and $\displaystyle b$ are congruent modulo $\displaystyle n$ when $\displaystyle a - b$ is a multiple of $\displaystyle n$. In other words, $\displaystyle a \equiv b \pmod{n}$ when $\displaystyle \frac{a-b}{n}$ is an integer. Otherwise, $\displaystyle a \not\equiv b \pmod{n}$, which means that $\displaystyle a$ and $\displaystyle b$ are not congruent modulo $\displaystyle n$.

### Examples

• $\displaystyle 31 \equiv 1 \pmod{10}$ because $\displaystyle 31 - 1 = 30$ is a multiple of $\displaystyle 10$

• $\displaystyle 43 \equiv 22 \pmod{7}$ because $\displaystyle \frac{43 - 22}{7} = \frac{21}{7} = 3$, which is an integer.

• $\displaystyle 8 \not\equiv -8 \pmod{3}$ because $\displaystyle 8 - (-8) = 16$, which is not a multiple of $\displaystyle 3$.

• $\displaystyle 91 \not\equiv 18 \pmod{6}$ because $\displaystyle \frac{91 - 18}{6} = \frac{73}{6}$, which is not an integer.

## Arithmetic Modulo n

### Useful Facts

Consider four integers ${a},{b},{c},{d}$ and a positive integer ${m}$ such that $a\equiv b\pmod {m}$ and $c\equiv d\pmod {m}$. In modular arithmetic, the following identities hold:

• Addition: $a+c\equiv b+d\pmod {m}$.
• Subtraction: $a-c\equiv b-d\pmod {m}$.
• Multiplication: $ac\equiv bd\pmod {m}$.
• Division: $\frac{a}{e}\equiv \frac{b}{e}\pmod {\frac{m}{\gcd(m,e)}}$, where $e$ is a positive integer that divides ${a}$ and $b$.
• Exponentiation: $a^e\equiv b^e\pmod {m}$ where $e$ is a positive integer.

### The Natural Appeal of Modular Arithmetic

Observe that we use modular arithmetic even when solving some of the most basic, everyday problems. For example:

Cody is cramming for an exam that will be held at 2 PM. It is the morning of the day of the exam, and Cody did not get any sleep during the night. He knows that it will take him exactly one hour to get to school from the time he wakes up, and he insists upon getting at least five hours of sleep. At what time in the morning should Cody stop studying and go to sleep?

We know that the hours of the day are numbered from $1$ to $12$, with hours having the same number if and only if they are a multiple of $12$ hours apart. So we can use subtraction mod $12$ to answer this question.

We know that since Cody needs five hours of sleep plus one hour to get to school, he must stop studying six hours before the exam. We can find out what time this is by performing the subtraction

$\overline{2} - \overline{6} = \overline{-4} = \overline{8}.$

So Cody must quit studying at 8 AM.

Of course, we are able to perform calculations like this routinely without a formal understanding of modular arithmetic. One reason for this is that the way we keep time gives us a natural model for addition and subtraction in $\mathbb{Z}_n$: a "number circle." Just as we model addition and subtraction by moving along a number line, we can model addition and subtraction mod $n$ by moving along the circumference of a circle. Even though most of us never learn about modular arithmetic in school, we master this computational model at a very early age.

#### A Word of Caution

Because of the way we define operations in $\mathbb{Z}_n$, it is important to check that these operations are well-defined. This is because each of the sets that make up $\mathbb{Z}_n$ contains many different numbers, and therefore has many different names. For example, observe that in $\mathbb{Z}_7$, we have $\overline{1} = \overline{8}$ and $\overline{2} = \overline{9}$. It is reasonable to expect that if we perform the addition $\overline{8} + \overline{9}$, we should get the same answer as if we compute $\overline{1} + \overline{2}$, since we are simply using different names for the same objects. Indeed, the first addition yields the sum $\overline{17} = \overline{3}$, which is the same as the result of the second addition.

The "Useful Facts" above are the key to understanding why our operations yield the same results even when we use different names for the same sets. The task of checking that an operation or function is well-defined, is one of the most important basic techniques in abstract algebra.

### Computation of Powers Mod n

The "exponentiation" property given above allows us to perform rapid calculations modulo $n$. Consider, for example, the problem

What are the tens and units digits of $7^{1942}$?

We could (in theory) solve this problem by trying to compute $7^{1942}$, but this would be extremely time-consuming. Moreover, it would give us much more information than we need. Since we want only the tens and units digits of the number in question, it suffices to find the remainder when the number is divided by $100$. In other words, all of the information we need can be found using arithmetic mod $100$.

We begin by writing down the first few powers of $\overline{7}$:

$\overline{7}, \overline{49}, \overline{43}, \overline{1}, \overline{7}, \overline{49}, \overline{43}, \overline{1}, \ldots$

A pattern emerges! We see that $7^4 = 2401 \equiv 1$ (mod $100$). So for any positive integer $k$, we have $7^{4k} = (7^4)^k \equiv 1^k \equiv 1$ (mod $100$). In particular, we can write

$7^{1940} = 7^{4 \cdot 485} \equiv 1$ (mod $100$).

By the "multiplication" property above, then, it follows that

$7^{1942} = 7^{1940} \cdot 7^2 \equiv 1 \cdot 7^2 \equiv 49$ (mod $100$).

Therefore, by the definition of congruence, $7^{1942}$ differs from $49$ by a multiple of $100$. Since both integers are positive, this means that they share the same tens and units digits. Those digits are $4$ and $9$, respectively.

#### A General Algorithm

In the example above, we were fortunate to find a power of $7$ -- namely, $7^4$ -- that is congruent to $1$ mod $100$. What if we aren't this lucky? Suppose we want to solve the following problem:

What are the tens and units digits of $13^{404}$?

Again, we will solve this problem by computing $\overline{13}^{404}$ modulo $100$. The first few powers of $\overline{13}$ are

$\overline{13}, \overline{69}, \overline{97}, \overline{61}, \overline{93}, \ldots$

This time, no pattern jumps out at us. Is there a way we can find the $404^{th}$ power of $\overline{13}$ without taking this list all the way out to the $404^{th}$ term -- or even without patiently waiting for the list to yield a pattern?

Suppose we condense the list we started above; and instead of writing down all powers of $\overline{13}$, we write only the powers $\overline{13}^k$, where $k$ is a power of $2$. We have

$\overline{13}^1 = \overline{13}$

$\overline{13}^2 = \overline{69}$

$\overline{13}^4 = \overline{69}^2 = \overline{61}$

$\overline{13}^8 = \overline{61}^2 = \overline{21}$

$\overline{13}^{16} = \overline{21}^2 = \overline{41}$

$\overline{13}^{32} = \overline{41}^2 = \overline{81}$

$\overline{13}^{64} = \overline{81}^2 = \overline{61}$

$\overline{13}^{128} = \overline{61}^2 = \overline{21}$

$\overline{13}^{256} = \overline{21}^2 = \overline{41}$

(Observe that this process yields a pattern of its own, if we carry it out far enough!)

Now, observe that, like any positive integer, $404$ can be written as a sum of powers of two:

$404 = 256 + 128 + 16 + 4$

We can now use this powers-of-two expansion to compute $\overline{13}^{404}$:

$\overline{13}^{404} = \overline{13}^{256} \cdot \overline{13}^{128} \cdot \overline{13}^{16} \cdot \overline{13}^4 = \overline{41} \cdot \overline{21} \cdot \overline{41} \cdot \overline{61} = \overline{61}.$

So the tens and units digits of $13^{404}$ are $6$ and $1$, respectively.

We can use this method to compute $M^e$ modulo $n$, for any integers $M$ and $e$, with $e > 0$. The beauty of this algorithm is that the process takes, at most, approximately $2 \log_2 e$ steps -- at most $\log_2 e$ steps to compute the values $\overline{M}^k$ for $k$ a power of two less than $e$, and at most $\log_2 e$ steps to multiply the appropriate powers of $\overline{M}$ according to the binary representation of $e$.

This method can be further refined using Euler's Totient Theorem.

## Applications of Modular Arithmetic

Modular arithmetic is an extremely flexible problem solving tool. The following topics are just a few applications and extensions of its use:

Invalid username
Login to AoPS