CS 702
Discrete Mathematics and Probability Theory
Spring 2009
Alistair Sinclair, David Tse
Note 5
Modular Arithmetic
One way to think of modular arithmetic is that it limits numbers to a predefined range
{
0
,
1
,...,
N

1
}
, and
wraps around whenever you try to leave this range — like the hand of a clock (where
N
=
12) or the days
of the week (where
N
=
7).
Example: Calculating the day of the week.
Suppose that you have mapped the sequence of days of
the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday) to the sequence of numbers
(
0
,
1
,
2
,
3
,
4
,
5
,
6
)
so that Sunday is 0, Monday is 1, etc. Suppose that today is Thursday (=4), and you want
to calculate what day of the week will be 10 days from now. Intuitively, the answer is the remainder of
4
+
10
=
14 when divided by 7, that is, 0 —Sunday. In fact, it makes little sense to add a number like 10 in
this context, you should probably find
its
remainder modulo 7, namely 3, and then add this to 4, to find 7,
which is 0.
What if we want to continue this in 10 day jumps? After 5 such jumps, we would have day 4
+
3
·
5
=
19
,
which gives 5 modulo 7 (Friday).
This example shows that in certain circumstances it makes sense to do arithmetic within the confines of
a particular number (7 in this example), that is, to do arithmetic by always finding the remainder of each
number modulo 7, say, and repeating this for the results, and so on. As well as being efficient in the sense of
keeping intermediate values as small as possible, this actually has several important applications, including
errorcorrecting codes and cryptography, as we shall see later.
To define things more formally, for any integer
m
(such as 7) we say that
x
and
y
are
congruent modulo m
if
they differ by a multiple of
m
, or in symbols,
x
=
y
mod
m
⇔
m
divides
(
x

y
)
.
For example, 29
=
5 mod 12 because 29

5 is a multiple of 12. We can also write 22
=

2 mod 12. Equiv
alently,
x
and
y
are congruent modulo
m
iff they have the same remainder modulo
m
. Notice that “congruent
modulo
m
” is an
equivalence relation
: it partitions the integers into
m
equivalence classes 0
,
1
,
2
,...,
m

1.
When computing modulo
m
, it is often convenient to reduce any intermediate results mod
m
to simplify the
calculation, as we did in the example above. This is justified by the following claim:
Theorem 5.1
:
If a
=
c
mod
m and b
=
d
mod
m, then a
+
b
=
c
+
d
mod
m and a
·
b
=
c
·
d
mod
m.
Proof
: We know that
c
=
a
+
k
·
m
and
d
=
b
+
l
·
m
, so
c
+
d
=
a
+
k
·
m
+
b
+
l
·
m
=
a
+
b
+(
k
+
l
)
·
m
,
which means that
a
+
b
=
c
+
d
mod
m
. The proof for multiplication is similar and left as an exercise.
a
What this theorem tells us is that we can always reduce any arithmetic expression modulo
m
into a natural
number smaller than
m
. As an example, consider the expresion
(
13
+
11
)
·
18 mod 7. Using the above
CS 702, Spring 2009, Note 5
1