CMSC 27100 — Lecture 10

Modular arithmetic

Now, armed with our accumulated knowledge on division, divisors, remainders, primes, and sets, we'll define a system of arithmetic on sets of integers based around remainders, which seems like a strange thing to do.

Often times, we want to do calculations based around multiples of certain numbers, like time—we represent time in "chunks". For instance, we group 60 seconds together into a minute, 60 minutes together into an hour, and 24 hours into a day, which we sometimes further split up into chunks of 12. We can conveniently represent these chunks together on one circle.

Modular arithmetic formalizes these notions. One of the things we'll see is that in certain cases, working in these structures gives us a notion of "division" that is well-defined. The system of modular arithmetic was first developed by Gauss.

Let $m$ be an integer. For integers $a$ and $b$, we say that $a$ is congruent to $b$ modulo $m$, written $a \equiv b \pmod m$ or $a \equiv_m b$, if $m \mid a-b$.

This definition looks a bit strange, but it really means that $a$ and $b$ have the same remainders when divided by $m$. You can convince yourself of this as an exercise (make use of the Division Theorem).

Ultimately, we want to be able to talk about integers that are equivalent to each other with respect to a particular modulus. An easy example of this is when we think about integers modulo 10, since our entire number system is built around 10s. We can formally define what it means to be "equivalent".

A relation $\sim$ is an equivalence relation if $\sim$ satisfies the following:

  1. Reflexivity: For all $a$, $a \sim a$.
  2. Symmetry: For all $a, b$, if $a \sim b$, then $b \sim a$.
  3. Transitivity: For all $a, b, c$, if $a \sim b$ and $b \sim c$, then $a \sim c$.

Equivalence relations are called as such because they capture relationships that are similar to, but not exactly, equality. For instance, if I have two propositional formulas $\varphi$ and $\neg \neg \varphi$, we can't say they're equal because they aren't: they contain different symbols and one is longer than the other. However, we can say that they're logically equivalent because they mean the same thing (in classical logic). If we really wanted to, we can define the notion of logical equivalence more formally and then show that it satisfies the conditions for equivalence relations.

For $m \gt 0$, $\equiv_m$ is an equivalence relation.

  1. To see that $\equiv_m$ is reflexive, observe that $m \mid a-a$ for all integers $a$.
  2. To see that $\equiv_m$ is symmetric, if $a \equiv_m b$, then $m \mid a - b$. This means there is an integer $n$ such that $a - b = mn$. Then we get $b - a = m\cdot -n$ and we have $m \mid b-a$.
  3. To see that $\equiv_m$ is transitive, consider integers $a,b,c$ such that $a \equiv_m b$ and $b \equiv_m c$. We have $m \mid a-b$ and $m \mid b-c$, which gives us $m \mid (a-b) + (b-c)$ and therefore, $m \mid a-c$ and $a \equiv_m c$.

 

Using the notion of an equivalence relation, we can divide $\mathbb Z$ into sets that contain equivalent members. For instance, if we choose $m = 2$, then all even numbers are equivalent to each other (i.e., $0 \pmod 2$) and all odd numbers are equivalent to each other (i.e., $1 \pmod 2$). These sets are called equivalence classes.

For all $m \gt 0$ and $a \in \mathbb Z$, we define the equivalence class modulo $m$ of $a$ to be the set of integers $$[a]_m = \{b \in \mathbb Z \mid b \equiv_m a\}.$$

Typically, we refer to equivalence classes by their "obvious" name, which is the member of the class that is between 0 and $m-1$. This is called the canonical representative of the class. Of course, we should keep in mind that $[0] = [m] = [2m] = [-m]$ and such. But in addition to this, sometimes the $[]_m$ gets dropped for convenience's sake and we have to determine from context whether "2" means $2 \in \mathbb Z$ or $[2]_m$. Usually, this becomes clear with the usage of $\pmod m$ and we will try to make that explicit, but outside of this course, that's not always guaranteed.

There is a lot of notation here that means almost the same thing, so let's take a moment to settle it all.

Here is something worth higlighting about equivalence classes that may seem obvious from the definitions. The equivalence classes modulo $m$ partition the set of integers.

Let $\mathcal U$ be a universe and let $A_1, A_2, \dots, A_n$ be sets in $\mathcal U$. Then the $A_1, \dots, A_n$ partition $\mathcal U$ if

What this means is that the equivalence classes split up the integers into these classes and every integer belongs in exactly one of these equivalence classes. If you think about it, this makes sense—every integer $n$ can be written as $n = q \cdot m + r$, and $r$ is guaranteed to exist and be unique with respect to $m$.

Where are we headed with this? We would like to do arithmetic on these things.

We define operations $+$ and $\cdot$ on the equivalence classes of $m$ by

All of this seems a bit obvious, but just like we did when we were defining addition on the natural numbers, we should think about what we're really doing here. We've defined operations $+$ and $\cdot$ that look like our usual operations on the integers. However, observe that we're not adding and multiplying integers; we've defined a notion of adding and multiplying sets of integers.

Based solely on this, there is no reason that what we've defined is guaranteed to work. For instance, how do we know that when adding two sets in this way that we even get a set that makes sense at all? So of course, we have to prove this and it will turn out that our definitions of equivalence classes and addition and multiplication on those classes is such that everything works out intuitively almost without a second thought.

We will only show that for $a_1 \equiv a_2 \pmod m$ and $b_1 \equiv b_2 \pmod m$, we have $a_1 + b_1 \equiv a_2 + b_2 \pmod m$. Multiplication is left as an exercise.

By definition, we have that $m \mid a_1 - a_2$ and $m \mid b_1 - b_2$. Then we have $m \mid (a_1 - a_2) + (b_1 - b_2)$. We can easily rearrange this to get $m \mid (a_1 + b_1) - (a_2 + b_2)$ and therefore, $a_1 + b_1 \equiv a_2 + b_2 \pmod m$.

Now, we can define our structure.

Let $\mathbb Z_m = \{[0]_m, [1]_m, \dots, [m-1]_m\}$. The integers mod $m$ is the set $\mathbb Z_m$, together with the binary operations $+$ and $\cdot$. The integers mod $m$ are denoted by $\mathbb Z/\equiv_m$ or simply as $\mathbb Z_m$.

Notice again that the while the elements of $\mathbb Z$ are numbers, the elements of $\mathbb Z_m$ are sets of integers—specifically equivalence classes.

Up to now, we have been working implicitly in the structure $\mathbb Z$, the integers. As I've briefly alluded to before, we're not only talking about the domain $\mathbb Z$ but also how we interpret operations like $+$ and $\cdot$. The integers mod $m$, $\mathbb Z_m$, is another structure, whose basic elements are the equivalence classes with respect to $\equiv_m$.

These kinds of structures—a set together with binary operations $+$ and $\cdot$ and identities for both operations—are called rings.

The notation $\mathbb Z/\equiv_m$ gives us a hint at what's happening. We took $\mathbb Z$ and partitioned it into equivalence classes by the relation $\equiv_m$. This idea of taking an algebraic structure and constructing another structure based on an equivalence relation is something that comes up a lot in algebra and is called a quotient structure, where quotient in the algebraic context just means equivalence class.

An observation one can make here is that the result of doing this seems to be that all we're doing is taking our numbers and treating them as though they are just the respective remainders. And this is what ends up happening in practice. But there's something subtle going on here, which is that we're really implicitly jumping back and forth between different algebraic structures (the integers, $\mathbb Z$, and the integers modulo $m$, $\mathbb Z_m$).

Multiplicative inverses in $\bmod m$

I mentioned earlier that one of the things that this structure allows us to do is, under certain conditions, "divide" things in the sense that there is an operation that we can perform on elements of our structure that "reverse" multiplication. I say "divide" because the operation that we perform is not really division. It's more accurate to say that we'll be showing that multiplicative inverses exist.

If integers $m \gt 0$ and $a$ are relatively prime, then $a$ has a multiplicative inverse mod $m$. That is, there exists an integer $b$ such that $a \cdot b = 1 \pmod m$.

This result says something interesting. First, multiplicative inverses in the integers modulo $m$ exist. However, they exist under certain conditions. This can be demonstrated via the following example.

Consider $\mathbb Z_4$. There are four equivalence classes: $0,1,2,3$. Since 1 and 3 are coprime, they have inverses: $1^{-1} \equiv 1$ (this is obvious) and $3^{-1} \equiv 3$, which we get by observing that $3 \cdot 3 \equiv 9 \equiv 1 \pmod 4$ or $3 \equiv -1 \pmod 4$ and $-1 \cdot -1 \equiv 1 \pmod 4$. However, 2 has no inverse: \begin{align*} 2 \cdot 0 &\equiv 0 &\pmod 4 \\ 2 \cdot 1 &\equiv 2 &\pmod 4 \\ 2 \cdot 2 &\equiv 4 \equiv 0 &\pmod 4 \\ 2 \cdot 3 &\equiv 6 \equiv 2 &\pmod 4 \end{align*}