# Modular arithmetic/Introduction

Modular arithmetic is a special type of arithmetic that involves only integers. This goal of this article is to explain the basics of modular arithmetic while presenting a progression of more difficult and more interesting problems that are easily solved using modular arithmetic.

## Motivation

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

$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 $1$ to $11$, we arrive back at $0$. The same is true in any other modulus (modular arithmetic system). In modulo $5$, we count

$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 $-12$ to $0$, when written in modulo 5, are

$3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0,$

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

$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.

### Sample Problem

Find the modulo $\displaystyle 4$ residue of $\displaystyle 311$.

#### Solution:

Since $\displaystyle 311 \div 4 = 77$ R $\displaystyle 3$, we know that

$\displaystyle 311 \equiv 3 \pmod{4}$

and $\displaystyle 3$ is the modulo $\displaystyle 4$ residue of $\displaystyle 311$.

## Making Computation Easier

We don't always need to perform tedious computations to discover solutions to interesting problems. If all we need to know about are remainders when integers are divided by $n$, then we can work directly with those remainders in modulo $n$. This can be more easily understood with a few examples.

#### Problem

Suppose we want to find the units digit of the following sum:

$2403 + 791 + 688 + 4339.$

We could find their sum, which is $8221$, and note that the units digit is $1$. However, we could find the units digit with far less calculation.

#### Solution

We can simply add the units digits of the summands:

$3 + 1 + 8 + 9 = 21.$

The units digit of this sum is $1$, which must be the same as the units digit of the four-digit sum we computed earlier.

#### Why we only need to use remainders

We can rewrite each of the integers in terms of multiples of $10$ and remainders:
$2403 = 240 \cdot 10 + 3$
$791 = 79 \cdot 10 + 1$
$688 = 68 \cdot 10 + 8$
$4339 = 433 \cdot 10 + 9$.
When we add all four integers, we get

$(240 \cdot 10 + 3) + (79 \cdot 10 + 1) + (68 \cdot 10 + 8) + (433 \cdot 10 + 9)$
$= (240 + 79 + 68 + 433) \cdot 10 + (3 + 1 + 8 + 9)$

At this point, we already see the units digits grouped apart and added to a multiple of $10$ (which will not affect the units digit of the sum):

$= 820 \cdot 10 + 21 = 8200 + 21 = 8221$.

#### Solution using modular arithmetic

Now let's look back at this solution, using modular arithmetic from the start. Note that
$2403 \equiv 3 \pmod{10}$
$791 \equiv 1 \pmod{10}$
$688 \equiv 8 \pmod{10}$
$4339 \equiv 9 \pmod{10}$
Because we only need the modulo $10$ residue of the sum, we add just the residues of the summands:

$2403 + 791 + 688 + 4339 \equiv 3 + 1 + 8 + 9 \equiv 21 \equiv 1 \pmod{10},$

so the units digit of the sum is just $1$.

In general, when $a, b, c$, and $d$ are integers and $m$ is a positive integer such that

$a \equiv c \pmod{m}$
$b \equiv d \pmod{m}$

the following is always true:

$a + b \equiv c + d \pmod{m}$.

And as we did in the problem above, we can apply more pairs of equivalent integers to both sides, just repeating this simple principle.

Let $a-c=m\cdot k$, and $b-d=m\cdot l$ for $l,k \in \mathbb{Z}$. Adding the two equations we get:

$\begin{eqnarray*} mk+ml&=&(a-c)+(b-d)\\ m(k+l)&=&(a+b)-(c+d) \end{enarray*}$ (Error compiling LaTeX. ! Missing \endgroup inserted.)

Which is equivalent to saying $a+b\equiv c+d\pmod{m}$

### Subtraction

The same shortcut that works with addition of remainders works also with subtraction.

#### Problem

Find the remainder when the difference between $60002$ and $601$ is divided by $6$.

#### Solution

Note that $60002 = 10000 \cdot 6 + 2$ and $601 = 100 \cdot 6 + 1$. So,
$60002 \equiv 2 \pmod{6}$
$601 \equiv 1 \pmod{6}$
Thus,

$60002 - 601 \equiv 2 - 1 \equiv 1 \pmod{6},$

so 1 is the remainder when the difference is divided by $6$. (Perform the subtraction yourself, divide by $6$, and see!)

#### Subtraction rule

When $a, b, c$, and $d$ are integers and $m$ is a positive integer such that

$a \equiv c \pmod{m}$
$b \equiv d \pmod{m}$

the following is always true:

$a - b \equiv c - d \pmod{m}$

.

### Multiplication

Modular arithmetic provides an even larger advantage when multiplying than when adding or subtracting. Let's take a look at a problem that demonstrates the point.

#### Problem

Jerry has 44 boxes of soda in his truck. The cans of soda in each box are packed oddly so that there are 113 cans of soda in each box. Jerry plans to pack the sodas into cases of 12 cans to sell. After making as many complete cases as possible, how many sodas will Jerry have leftover?

#### Solution

First, we note that this word problem is asking us to find the remainder when the product $44 \cdot 113$ is divided by $12$.

Now, we can write each $44$ and $113$ in terms of multiples of $12$ and remainders:
$44 = 3 \cdot 12 + 8$
$113 = 9 \cdot 12 + 5$
This gives us a nice way to view their product:

$44 \cdot 113 = (3 \cdot 12 + 8)(9 \cdot 12 + 5)$
$(3 \cdot 9) \cdot 12^2 + (3 \cdot 5 + 8 \cdot 9) \cdot 12 + (8 \cdot 5)$

We can already see that each part of the product is a multiple of $12$, except the product of the remainders when each $44$ and $113$ are divided by 12. That part of the product is $8 \cdot 5 = 40$, which leaves a remainder of $4$ when divided by $12$. So, Jerry has $4$ sodas leftover after making as many cases of $12$ as possible.

#### Solution using modular arithmetic

First, we note that
$44 \equiv 8 \pmod{12}$
$113 \equiv 5 \pmod{12}$
Thus,

$44 \cdot 113 \equiv 8 \cdot 5 \equiv 40 \equiv 4 \pmod{12},$

meaning there are $4$ sodas leftover. Yeah, that was much easier.

#### Multiplication rule

When $a, b, c$, and $d$ are integers and $m$ is a positive integer such that

$a \equiv c \pmod{m}$
$b \equiv d \pmod{m}$

The following is always true:

$a \cdot b \equiv c \cdot d \pmod{m}$.

### Exponentiation

Since exponentiation is just repeated multiplication, it makes sense that modular arithmetic would make many problems involving exponents easier. In fact, the advantage in computation is even larger and we explore it a great deal more in the intermediate modular arithmetic article.

Note to everybody: Exponentiation is very useful as in the following problem:

#### Problem #1

What are the last two digits of $(...((7)^7)^7)...)^7$ if there are 1000 7s as exponents and only one 7 in the middle?

We could solve this problem using mods. This can also be stated as $7^{7^{1000}}$. After that, we see that 7 is congruent to -1 in mod 4, so we can use this fact to replace the 7s with -1s, because 7 has a pattern of repetitive period 4 for the units digit. $(-1)^{1000}$ is simply 1, so therefore $7^1=7$, which really is the last digit.

#### Problem #2

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 $7$ mod $100$:

$7, 49, 43, 1, 7, 49, 43, 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.

#### Problem #3

Can you find a number that is both a muliple of $2$ but not a multiple of $4$ and a perfect square?

#### Solution to Problem #3

No you cannot, rewritting the question we see that it asks us to find a number $n$ that satisfies this: $4n+2=x^2$.

Taking mod $4$ on both sides we find that $x^2\equiv 2\pmod{4}$, now all we are missing to show is that no matter what $x$ is, $x^2$ will never be a multiple of $4$ plus $2$, so we work with cases:

$x\equiv 0\pmod{4}\implies x^2\equiv 0\pmod{4}$

$x\equiv 1\pmod{4}\implies x^2\equiv 1\pmod{4}$

$x\equiv 2\pmod{4}\implies x^2\equiv 4\equiv 0\pmod{4}$

$x\equiv 3\pmod{4}\implies x^2\equiv 9\equiv 1\pmod{4}$

This assures us that it is impossible to find such a number.

## Summary of 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.

## 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: