# Modular arithmetic/Intermediate

Given integers $a$, $b$, and $n$, with $n > 0$, we say that $a$ is congruent to $b$ modulo $n$, or $a \equiv b$ (mod $n$), if the difference ${a - b}$ is divisible by $n$.

For a given positive integer $n$, the relation $a \equiv b$ (mod $n$) is an equivalence relation on the set of integers. This relation gives rise to an algebraic structure called the integers modulo $n$ (usually known as "the integers mod $n$," or $\mathbb{Z}_n$ for short). This structure gives us a useful tool for solving a wide range of number-theoretic problems, including finding solutions to Diophantine equations, testing whether certain large numbers are prime, and even some problems in cryptology.

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

For examples, see Introduction to modular arithmetic.

### Addition, Subtraction, and Multiplication Mod n

We define addition, subtraction, and multiplication in $\mathbb{Z}_n$ according to the following rules:

$\overline{a} + \overline{b} = \overline{a+b}$ for all $a, b \in \mathbb{Z}$. (Addition)

$\overline{a} - \overline{b} = \overline{a-b}$ for all $a, b \in \mathbb{Z}$. (Subtraction)

$\overline{a} \cdot \overline{b} = \overline{ab}$ for all $a, b \in \mathbb{Z}$. (Multiplication)

So for example, if $n = 7$, then we have

$\overline{3} + \overline{2} = \overline{3+2} = \overline{5}$

$\overline{4} + \overline{4} = \overline{4+4} = \overline{8} = \overline{1}$

$\overline{4} \cdot \overline{3} = \overline{4 \cdot 3} = \overline{12} = \overline{5}$

$\overline{6} \cdot \overline{6} = \overline{6 \cdot 6} = \overline{36} = \overline{1}$

Notice that, in each case, we reduce to an answer of the form $\overline{k}$, where $0 \leq k < 7$. We do this for two reasons: to keep possible future calculations as manageable as possible, and to emphasize the point that each expression takes one of only seven (or in general, $n$) possible values. (Some people find it useful to reduce an answer such as $\overline{5}$ to $\overline{-2}$, which is negative but has a smaller absolute value.)

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

## Algebraic Properties of the Integers Mod n

The integers modulo $n$ form an algebraic structure called a ring -- a structure in which we can add, subtract, and multiply elements.

Anyone who has taken a high school algebra class is familiar with several examples of rings, including the ring of integers, the ring of rational numbers, and the ring of real numbers. The ring $\mathbb{Z}_n$ has some algebraic features that make it quite different from the more familiar rings listed above.

First of all, notice that if we choose a nonzero element $\overline{a}$ of $\mathbb{Z}_n$, and add $n$ copies of this element, we get

$\overline{a} + \overline{a} + \cdots + \overline{a} = n \cdot \overline{a} = \overline{na} = \overline{0}$,

since $na$ is a multiple of $n$. So it is possible to add several copies of a nonzero element of $\mathbb{Z}_n$ and get zero. This phenomenon, which is called torsion, does not occur in the reals, the rationals, or the integers.

Another curious feature of $\mathbb{Z}_n$ is that a polynomial over $\mathbb{Z}_n$ can have a number of roots greater than its degree. Consider, for example, the polynomial congruence

$x^2 - 2x - 15 \equiv 0 \pmod{21}$.

We might be tempted to solve this congruence by factoring the expression on the left:

$(x - 5)(x + 3) \equiv 0 \pmod{21}$.

Indeed, this factorization yields two solutions to the congruence: $x \equiv 5 \pmod{21}$, and $x \equiv -3 \equiv 18 \pmod{21}$. (Note that two values of $x$ that are congruent modulo $21$ are considered the same solution.)

However, since $15 \equiv 99 \pmod{21}$, the original congruence is equivalent to

$x^2 - 2x - 99 \equiv 0 \pmod{21}$.

This time, factoring the expression on the left yields

$(x - 11)(x + 9) \equiv 0 \pmod{21}$.

And we find that there are two more solutions! The values $x \equiv 11 \pmod{21}$ and $x \equiv -9 \equiv 12 \pmod{21}$ both solve the congruence. So our congruence has at least four solutions -- two more than we might expect based on the degree of the polynomial.

Why do the "rules" of algebra that work so well for the real numbers seem to fail in $\mathbb{Z}_{21}$? To understand this, let's take a closer look at the congruence

$(x - 5)(x + 3) \equiv 0 \pmod{21}$.

If we were solving this as an equation over the reals, we would immediately conclude that either $x - 5$ must be zero, or $x + 3$ must be zero in order for the product to equal zero. However, this is not the case in $\mathbb{Z}_{21}$! It is possible to multiply two nonzero elements of $\mathbb{Z}_{21}$ and get zero. For example, we have

$\overline{3} \cdot \overline{7} = \overline{0}$

$\overline{9} \cdot \overline{7} = \overline{0}$

$\overline{6} \cdot \overline{14} = \overline{0}$

But wait! Suppose we take a close look at this last product, and we set $x - 5 \equiv 6 \pmod{21}$ and $x + 3 \equiv 14 \pmod{21}$. Then we have $x \equiv 11 \pmod{21}$ -- another of the solutions of our congruence! (One can check that the other two factorizations don't lead to any valid solutions; however, there are many other factorizations of zero that need to be checked.)

In the ring of real numbers, it is a well-known fact that if $ab = 0$, then $a = 0$ or $b = 0$. For this reason, we call the ring of real numbers a domain. However, a similar fact does not apply in general in $\mathbb{Z}_n$; therefore, $\mathbb{Z}_n$ is not in general a domain.

## Topics

The following topics expand on the flexible nature of modular arithmetic as a problem solving tool:

## Miscellaneous

### The binary operation "mod"

Related to the concept of congruence, mod $n$ is the binary operation $a$ mod $n$, which is used often in computer programming.

Recall that, by the Division Algorithm, given any two integers $a$ and $n$, with $n > 0$, we can find integers $q$ and $r$, with $0 \leq r < n$, such that $a = nq + r$. The number $q$ is called the quotient, and the number $r$ is called the remainder. The operation $a$ mod $n$ returns the value of the remainder $r$. For example:

$15$ mod $6 = 3$, since $15 = 6 \cdot 2 + 3$.

$35$ mod $7 = 0$, since $35 = 7 \cdot 5 + 0$.

$-10$ mod $8 = 6$, since $-10 = 8 \cdot -2 + 6$.

Observe that if $a$ mod $n = r$, then we also have $a \equiv r$ (mod $n$).

An example exercise with modular arithmetic:

Problem:

Let

$D=d_1d_2d_3d_4d_5d_6d_7d_8d_9$

be a nine-digit positive integer (each digit not necessarily distinct). Consider

$E=e_1e_2e_3e_4e_5e_6e_7e_8e_9$,

another nine-digit positive integer with the property that each digit $e_i$ when substituted for $d_i$ makes the modified D divisible by 7. Let

$F=f_1f_2f_3f_4f_5f_6f_7f_8f_9$ be a third nine-digit positive integer with the same relation to E as E has to D.

Prove that every $d_i - f_i$ is divisible by 7.

Solution:

Any positive integer $D=d_1d_2d_3d_4d_5d_6d_7d_8d_9$ can be expressed $(10^8)d_1+(10^7)d_2+...(10^0)d_9$.

Since 10=3 mod 7, and since it holds that if a=b mod c then $a^n=b^n$ mod c, then D can be expressed much more simply mod 7; that is, $D= 2d_1 +3d_2 +1d_3 -2d_4 -3d_5 -d_6 +2d_7 +3d_8 +d_9$= x mod 7.

Each number in E must make the modified D equal 0 mod 7, so for each $d_i$, $e_i = \frac{x+7k}{c}-d_i$, where c is the coefficient of $d_i$ and k is an element of {-2,-1,0,1,2}. The patient reader should feel free to verify that this makes D = 0 mod 7.

In terms of $d_i$ terms, then, we find each $c_ie_i = x - c_id_i + 7k$.

Then $E= 2e_1 +3e_2 +1e_3 -2e_4 -3e_5 -e_6 +2e_7 +3e_8 +e_9$ mod 7 can be expressed $E= (x-2d_1)+(x-3d_2)+(x-d_3)+...+(x-d_9) = (9x)-D$ mod 7 = (9x)- x = 8x = x mod 7. (Note that the 7s, which do not change the mod value, have been eliminated.)

Each number in F must make the modified E equal 0 mod 7, so for each $e_i$, $f_i = \frac{x+7k_2}{c_i} -e_i = \frac{x+7k_2}{c_i} - (\frac{x+7k_1}{c_i} -d_i)$.

By design and selection of k, all $(f_i)$ are integers, and $d_i - f_i$ is always an integer because it is the difference of two integers.

$d_i - f_i = \frac{7k_2-7k_1}{c_i}$

$c_i$ is a member of the set {1, 2, 3}. Since no $c_i$ divides 7, 7 may be factored and $7\frac{k_2-k_1}{c_i} = d_i - f_i$ is the product of two integers.

Let $A=\frac{k_2-k_1}{c_i}$ then $d_i - f_i =$ 7A mod 7 = 0 mod 7 for all $(d_i,f_i)$, QED.