CMSC 27100 — Lecture 5

The notes for this course began from a series originally written by Tim Ng, with extensions by David Cash and Robert Rand, adapted for the summer session by Jonathan Liu. I have modified them to follow our course.

Modular Arithmetic

For Euclid's Algorithm, we introduced the modulus operator:

Let $n$ and $d \gt 0$ be integers. We define the functions $n \mathop{\mathbf{div}} d = q$ and $n \mathop{\mathbf{mod}} d = r$, where $q$ and $r$ are as defined by the Division Theorem.

In this lecture we'll introduce a system of modular arithmetic on integers based around this operator that has a wide array of applications. In particular we'll develop a notion of "division" that is well-defined within this system. The system of modular arithmetic was first developed by Gauss.

Let $m$ be a positive 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)$. We write $a \not\equiv_m b$ to mean that $a$ is not congruent to $b$ modulo $m$.

We will prefer the notation $a \equiv_m b$.

We need to be careful to distinguish the notion of equivalence modulo $m$ and the function $a \mathop{\mathbf{mod}} m$. In the definition above, $a \equiv_m b$ is shorthand for the assertion that $m\mid (a-b)$. On the other hand, $a \mathop{\mathbf{mod}} m$ is a number. However, there is a tight connection between the two:

For all integers $a,b,m$ with $m$ positive, $a \equiv_m b$ if and only if $a \mathop{\mathbf{mod}} m = b \mathop{\mathbf{mod}} m$.

Remember that for "if and only if" statements, we need to prove both directions. Assume first that $a \equiv_m b$. By the Division Theorem applied twice, there exist $q_1,q_2,r_1,r_2\in\mathbb{Z}$ such that $a = q_1m + r_1$ and $b = q_2m + r_2$, and $0\leq r_1,r_2 \lt m$. Thus $a-b = (q_1 - q_2)m + (r_1-r_2)$. Since $a \equiv_m b$, we know that $m|(a-b)$. Combining this with the obvious fact that $m|(q_1-q_2)m$, we get that $m|(r_1-r_2)$. But $-m \lt r_1-r_2 \lt m$, and the only multiple of $m$ in that range is $0$. Hence $r_1-r_2=0$, i.e. $r_1=r_2$. Since, by definition, $a \mathop{\mathbf{mod}} m= r_1$ and $b \mathop{\mathbf{mod}} m = r_2$, we are done.

For the reverse direction, assume that $a \mathop{\mathbf{mod}} m = b \mathop{\mathbf{mod}} m$, and let $q_1,q_2,r_1,r_2\in\mathbb{Z}$ be as before. We then immediately have $r_1=r_2$ by the definitions of $r_1$ and $r_2$, so $a-b = (q_1 - q_2)m + (r_1-r_2)=(q_1-q_2)m$. Thus $m|(a-b)$, or in other words, $a \equiv_m b$.

We next give some simple properties of the $\equiv_m$ symbol which say that it behaves intuitively like standard equality.

Let $m$ be a positive integer.

We need to show that $\equiv_m$ is reflexive, symmetric, and transitive.

To see that $\equiv_m$ is reflexive, observe that $m \mid (a-a)$ for all integers $a$, showing that $a\equiv_m a$. 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 $mn= a - b$. Then we get $b - a = m\cdot (-n)$, showing $m \mid (b-a)$, i.e. $b\equiv_m a$.

To see that $\equiv_m$ is transitive, let $a,b,c$ be integers 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$. $$\tag*{$\Box$}$$

The proof of this proposition is a good exercise in using the definitions. The proposition itself allows one to make chains of assertions like $a\equiv_m b \equiv_m c$.

Next we state a proposition that allows one to substitute congruent values while doing addition and multiplication modulo $m$.

Let $m$ be a positive integer. If $a \equiv_m b$ and $c \equiv_m d$, then $a + c \equiv_m b + d$ and $ac \equiv_m bd$.

Let $m\mid a-b$ and $m\mid c-d$ for $m > 0$ and $a,b,c,d \in \mathbb{Z}$. Then $m\mid (a-b) + (c-d) = (a + c) - (b+d)$. By definition, $a + c \equiv_m b + d$. Next, note that if $m\mid a-b$ and $m\mid c-d$, then $m\mid c(a-b) + b(c-d) = ac - bd$, so $ac \equiv_m bd$.

Thinking Modularly

The practical consequence of all of these properties is that one can interleave addition and multiplication with "modding down" as one likes. Here is one way to state that.

Let $m$ be a positive integer. Then

$$ (a+b) \mathop{\mathbf{mod}} m = ((a\mathop{\mathbf{mod}} m) + (b\mathop{\mathbf{mod}} m)) \mathop{\mathbf{mod}} m, $$ and $$ ab \mathop{\mathbf{mod}} m = ((a\mathop{\mathbf{mod}} m) (b\mathop{\mathbf{mod}} m)) \mathop{\mathbf{mod}} m. $$

We'll prove the statement about addition and leave the second statement as an exercise. By Proposition 5.2, to show $(a+b) \mathop{\mathbf{mod}} m = ((a\mathop{\mathbf{mod}} m) + (b\mathop{\mathbf{mod}} m)) \mathop{\mathbf{mod}} m $, it is enough to show that $a+b \equiv_m (a\mathop{\mathbf{mod}} m)+(b\mathop{\mathbf{mod}} m)$. Using the definition of $\mathbf{mod}$, it is easy to show that $a \equiv_m (a \mathop{\mathbf{mod}} m)$ and $b \equiv_m (b \mathop{\mathbf{mod}} m)$. Proposition 5.4 allows us to add these congruences for the desired conclusion.

For instance, to compute $31\cdot(123+49)\mathop{\mathbf{mod}} 5$, you can reduce everything modulo $5$ at any time; Thus this value is

\begin{align} 31\cdot(123+49)\mathop{\mathbf{mod}} 5 & = (31 \mathop{\mathbf{mod}} 5)\cdot ((123 + 49) \mathop{\mathbf{mod}} 5) \mathop{\mathbf{mod}} 5 \\ & = (31 \mathop{\mathbf{mod}} 5)\cdot ((123 \mathop{\mathbf{mod}} 5) + (49 \mathop{\mathbf{mod}} 5)) \mathop{\mathbf{mod}} 5 \\ &= 1\cdot (3 + 4) \mathop{\mathbf{mod}} 5 \\ & = 7 \mathop{\mathbf{mod}} 5\\ &= 2. \end{align}

Note that these are equalities of integers (using the $=$ symbol) and not modular congruences (which would use the $\equiv_m$ symbol). In order to prove this, we used Proposition 5.5 twice (on the first second equalities; check that you can see why).

This can be intuitive once you're used to it, but you should be careful not to overstep what it is implied by the theorem. We can "mod down" when doing addition and multiplication, there are operations for which this isn't true, as the next example shows.

Observe that $16 \equiv_{10} 6$, but if we divide both sides by $2$ (as integers) then the result is not congruent modulo $10$: $16/2 =8$ and $6/2 = 3$, and $8 \not\equiv_{10} 3$.

For a similar example, we have that $16 \equiv_{12} 4$, but this congruence will not hold if we take the square root of both sides: $\sqrt{16} = 4$ and $\sqrt{4}=2$, and $4\not\equiv_{12} 2$.

Multiplicative Inverses

Next we develop a theory of how we can "divide by an integer $a$ modulo $m$". Of course, this language isn't defined so far, since we can't speak of "modding down" rational numbers. Instead we'll back up and reconsider what division should mean in this context.

Multiplicative Inverses Modulo $m$

Obviously we're not going to actually divide integers, since we don't want to create havoc with rational numbers. Instead, let's take a new perspective on what it means to divide by a number. Observe that if $a,b\in\mathbb{Z}$, then we can define "dividing by $a$" to be an operation that transforms $ab$ to $b$. Put another way, "dividing by $a$" can be phrased as "multiplying by $a^{-1}$". Under the usual interpretation, $a^{-1}$ is simply $1/a$, which is not an integer in general . Instead, we're going to define a suitable stand-in that adapts the following point of view: $a^{-1}$ is a number such that $a\cdot a^{-1}=1$.

This discussion motivates the following definition.

Let $m,a$ be integers. We say that another integer $b$ is a multiplicative inverse of $a$ modulo $m$ if $ab \equiv_m 1$. When $a$ has a multiplicative inverse modulo $m$, we say that $a$ is invertible modulo $m$.

When the context and modulus $m$ are clear, we will sometimes just call $b$ "an inverse of $a$". We may go even further and write $a^{-1}$ for an inverse of $a$, with the understanding this implicitly depends on the modulus $m$ (and definitely isn't a non-integer value!).

We have that $3$ is an inverse of $2$ modulo $5$, since $2\cdot 3 \equiv_5 1$. Notice also that $-2$ and $8$ are also inverses of $2$ modulo $5$.

Consider $m=4$. There are four possible values of $a \mathop{\mathbf{mod}} 4$: $0,1,2,3$. Observe that 1 and 3 have inverses modulo $4$: $1^{-1} \equiv_4 1$ and $3^{-1} \equiv_4 3$, which we get by observing that $3 \cdot 3 \equiv_4 9 \equiv_4 1$. However, a little searching turns up no inverse for 2:

\begin{align*} 2 \cdot 0 &\equiv_4 0 \\ 2 \cdot 1 &\equiv_4 2 \\ 2 \cdot 2 &\equiv_4 4 \equiv_4 0 \\ 2 \cdot 3 &\equiv_4 6 \equiv_4 2 \end{align*}

If you poke around though, you'll notice that similarly $6$ does not appear to have any inverses modulo $12$. There is pattern here, but it is non-obvious. To see it, we need the following definition.

Two integers $a$ and $b$ are relatively prime (or coprime) if $\gcd(a,b) = 1$.

The numbers 10 and 21 are relatively prime, since the divisors of $10$ are $\{\pm 1, \pm 2, \pm 5, \pm 10\}$ and $21$ are $\{\pm 1, \pm 3, \pm 7, \pm 21\}$ and their only common divisors are $\pm 1$, of which $1$ is the largest.

This theorem explains which integers have an inverse modulo $m$.

Let $m$ be a positive integer and $a$ be an integer. Then there exists a multiplicative inverse of $a$ modulo $m$ if and only if $a$ and $m$ are relatively prime.

Since this is an "if and only if" statement, we need to prove the implication in both directions. So let's first assume that $a$ and $m$ are relatively prime. By Bézout's Identity, there exist integers $s$ and $t$ such that $sa + tm = 1$. Then $tm = 1-sa$ shows that $m\mid 1-sa$, i.e. that $1 \equiv_m sa$. This shows that $s$ is an inverse of $a$ modulo $m$.

For the other direction, assume there exists multiplicative inverse of $a$ modulo $m$, say $b$. Then $m |(ab-1)$, so there exists $k\in\mathbb{Z}$ such that $km = ab-1$. This gives $ba+(-k)m=1$, showing that

$$ 1 \in \{sa + tm \ \mid \ s,t\in\mathbb{Z}\}. $$

By our proof of Bézout's Identity, we know that $\gcd(a,m)$ is the smallest positive element of this set, meaning that it must be $1$.

The numbers $1,5,7,11$ are invertible modulo $12$, and the integers $2,3,4,6,8,9,10$ are not.

Cancellation and Uniqueness of Inverses

One application of inverses is to solving "equations" like $$6x \equiv_{17} 8.$$ These are usually called "congruences" since they are not asserting that anything is equal. In any case, intuitively we want to "divide" both sides by $6$ to solve for $x$. The next theorem justifies using inverses for this.

Let $m$ be a positive integer. For all $a,b,c\in\mathbb{Z}$ such that $a$ is relatively prime to $m$, if $ab \equiv_m ac$, then $b \equiv_m c$.

This says that we can cancel $a$ from both sides. The analogous version of this from basic algebra says that if $a,b,c$ are real numbers such that $a$ is non-zero, then $ab=ac$ implies $b=c$. Note the connection between being non-zero and being invertible. In the case of real numbers, the conclusion may not be true of $a$ is zero. Similarly, when working modulo $m$ the conclusion may not be true if $a$ is not invertible. You should try out some examples, say with $m=12$ and $a=6,b=2,c=4$.

Since $a$ is relatively prime to $m$, it has an inverse modulo $m$. Then since $ab \equiv_m ac$, we have $a^{-1}ab \equiv_m a^{-1}ac$. But the left hand side is $a^{-1}ab \equiv_m 1\cdot b \equiv_m b$. Similarly, the right hand side is congruent to $c$ modulo $m$. Thus $b\equiv_m c$.

Note that proofs like this implicitly use multiple properties of$\equiv_m$. You're encouraged to check it carefully.

To solve the congruence $6x \equiv_{17} 8$ from above, observe that $3$ is an inverse of $6$ modulo $17$. Thus we can transform this to an equivalent congruence $3\cdot 6x \equiv_{17} 3\cdot 8$, or $x \equiv_{17} 24$. Thus $x=24$ is a solution, and modding down further gives $x=7$ as another solution. After working a few of these, they are not too hard (see Rosen, 8th edition, Section 4.4.2 for some more examples). It is however important to understand once how each of those steps is formally justified. (For example: Why is it the case that $3\cdot 6x \equiv_{17} 3\cdot 8$, i.e. why does multiplying by $3$ preserve equivalence?)