Modular Arithmetic PDF
Modular Arithmetic PDF
Modular Arithmetic
Definiton. Let a, b, and m be integers. a = b (mod m) (read “a equals b mod m” or a is congruent to b
mod m) if any of the following equivalent conditions hold:
(a) m | a − b.
(b) m | b − a.
m is called the modulus of the congruence. I will almost always work with positive moduli.
Note that a = 0 (mod m) if and only if m | a. Thus, modular arithmetic gives you another way of
dealing with divisibility relations. Another way of saying this is: Mod m any multiple of m is 0.
Remark. Many people prefer to write “a ≡ b (mod m)”. Since equality mod m is an equivalence relation,
since “=” is a little less writing than “≡”, and since there isn’t much risk of confusion, I’ll write “=”.
Example.
Proof. I’ll prove transitivity by way of example. Suppose a = b (mod m) and b = c (mod m). Then there
are integers j and k such that
a − b = jm, b − c = km.
Add the two equations:
a − c = (j + k)m.
This implies that a = c (mod m).
1
Note that you can use the second property and induction to show that if a = b (mod m), then
m | (a − b) + (c − d) = (a + c) − (b + d).
(b) m | a − b and m | c − d imply that there are integers j and k such that
a = b + mj and c = d + mk.
ac = (b + mj)(d + mk)
ac = bd + m(dj + bk + mjk)
ac − bd = m(dj + bk + mjk)
Proof. Apply the theorem to the equations a = b (mod m) and c = c (mod m).
Assume that the modulus m is a positive integer. By the Division Algorithm, every integer n can be
written as
n = qm + r where 0 ≤ r < m.
Reducing this equation mod m, I have qm = 0 (mod m), so
n = r (mod m) .
Since 0 ≤ r < m, I have r ∈ {0, 1, . . . m − 1}. In other words, mod m every integer can be reduced to
a number in {0, 1, . . . m − 1}. This set is called the standard residue system mod m, and answers to
modular arithmetic problems will usually be simplified to a number in this range.
Example. (a) What are the equivalence classes under the relation of congruence mod 3?
Each integer belongs to exactly one of these classes. Two integers in a given class are congruent mod 3.
(If you know some group theory, you probably recognize this as constructing Z3 from Z.)
(b) When you’re doing things mod 3, it is if there were only 3 numbers. I’ll grab one number from each of
the classes to represent the classes; for simplicity, I’ll use 0, 2, and 1.
2
Here is an addition table for the classes in terms of these representatives:
+ 0 1 2
0 0 1 2
1 1 2 0
2 2 0 1
Example. (Reducing an expression mod n) Reduce 1005 (mod 7) to an element in the standard residue
system {0, 1, . . . , 6}.
Example. Simplify 994 · 996 · 997 · 998 (mod 1000) to a number in the range {0, 1, . . . 999}.
Rather than deal with large “positive” numbers, I’ll convert them to small “negative” numbers:
994 = −6 (mod 1000) , 996 = −4 (mod 1000) , 997 = −3 (mod 1000) , 998 = −2 (mod 1000) .
So
994 · 996 · 997 · 998 = (−6)(−4)(−3)(−2) = 144 (mod 1000) .
(x + y)p = xp + y p (mod p) .
The result is not true if the modulus is not prime. For example,
3
Example. Prove that if x ∈ Z, then 4x2 + x + 3 is not divisible by 5.
The phrase “not divisible by 5” leads one to think of doing things mod 5.
Every integer is equal to one of 0, 1, 2, 3, or 4 mod 5. Make a table:
x (mod 5) 0 1 2 3 4
4x2 + x + 3 (mod 5) 3 3 1 2 1
From the table, 4x2 + x + 3 6= 0 (mod 5) for all x ∈ Z, so 5 6 | 4x2 + x + 3 for all x ∈ Z.
Example. Give a counterexample to show that a = b (mod n) does not imply that xa = xb (mod n), for
a, b, n, x ∈ Z.
For instance, 7 = 4 (mod 3), but 27 6= 24 (mod 3) (since 128 6= 16 (mod 3)).
6x + 1 = 2(x + 2) (mod 7)
6x + 1 = 2x + 4 (mod 7)
4x = 3 (mod 7)
If this were an equation over the real numbers, you could divide both sides by 4 — equivalently, multiply
1
both sides by .
4
1
What would “ ” mean mod 7? This is the multiplicative inverse of 4, which we write as 4−1 (in modular
4
arithmetic you don’t use fraction notation). This means: What number multiplied by 4 gives 1 mod 7?
Since there are only 7 numbers mod 7, I can do this by trial and error, multiplying 4 by 0, 1, . . . until I
get 1. I find that
2 · 4 = 8 = 1 (mod 7) .
So for this modular equation, I multiply both sides by 2:
2 · 4x = 2 · 3 (mod 7)
8x = 6 (mod 7)
x = 6 (mod 7)
You can see that finding multiplicative inverses mod n can be useful in solving congruences. Sometimes
they can be found by more refined trial and error than simply trying all the numbers mod n.
Since multiples of n equal 0 mod n,
1 = 1 + n = 1 + 2n = 1 + 3n = · · · (mod n) .
4
Example.
(c) Prove that 25 does not have a multiplicative inverse mod 30.
(a) I take multiples of 7 and add 1, stopping when I get a number which is divisible by 5:
7 + 1 = 8, but 5 6 | 8.
(b) I take multiples of 89 and add 1, stopping when I get a number which is divisible by 45:
(c) Suppose that 25x = 1 (mod 30) (so x = 25−1 (mod 30)). Then
This contradiction shows that there is no such x, so 25 does not have a multiplicative inverse mod 30.
The previous method still has its limitations, as you can see by trying to use it to find 47−1 (mod 61).
And as you saw, some elements don’t have multiplicative inverses mod n. The following theorem says which
elements have multiplicative inverses, and how to find them if they exist.
Proof. Suppose m has a multiplicative inverse mod n. This means that am = 1 (mod n) for some a. Then
am + bn = 1 for some b ∈ Z.
Hence, (m, n) = 1.
Conversely, if (m, n) = 1, then
am + bn = 1 for some a, b ∈ Z.
Reducing mod n, I get am = 1 (mod n), which means that m has a multiplicative inverse mod n.
As the proof shows, you can find a−1 (mod n) by applying the Extended Euclidean algorithm to a and
n.
Example. (Finding elements which have multiplicative inverses) Which elements of {0, 1, 2, . . . , 11}
have multiplicative inverses mod 12?
5
The numbers in {0, 1, 2, . . . , 11} which are relatively prime to 12 and 1, 5, 7, and 11. Hence, 1, 5, 7, and
11 have multiplicative inverses mod 12.
61 - 13
47 1 10
14 3 3
5 2 1
4 1 1
1 4 0
(−10) · 61 + 13 · 47 = 1
13 · 47 = 1 (mod 61)
Proof. Write
ac − bc = km, where k ∈ Z.
Then
c m
(a − b) =k .
(c, m) (c, m)
c m c
(Notice that and are integers, since (c, m) | c and (c, m) | m.) Now divides the
(c, m) (c, m) (c, m)
m
right side, but it’s relatively prime to . Therefore, it must divide k:
(c, m)
c
k= j for some j ∈ Z.
(c, m)
Hence,
c c m
(a − b) = j·
(c, m) (c, m) (c, m)
m
a−b=j·
(c, m)
m
Therefore, a = b mod .
(c, m)
Notice that you “divide the equality” by c, but you divide the modulus by (c, m).
6
Example. (Solving a congruence with cancellation) Solve
10 · 2x = 10 · 5 (mod 19)
20x = 50 (mod 19)
x = 12 (mod 19)
(If you didn’t see that 2−1 = 10 (mod 19) by trial, you’d use the Extended Euclidean algorithm as
before.)
The original congruence was mod 38, so I want all solutions in the range {0, 1, . . . 37. I have one: x = 12.
To get others, I add multiples of 19 until I exceed 37. Thus, x = 12 + 19 = 31 is the other solution.
All together, the solutions are x = 12 (mod 38) and x = 31 (mod 38).
Example. (A congruence with no solutions) Show that the following congruence has no solutions:
4x = 5 (mod 14) .
4x = 5 (mod 14)
7 · 4x = 7 · 5 (mod 14)
28x = 35 (mod 14)
0 = 7 (mod 14)
These examples show that linear congruences may have solutions or may be unsolvable. We can under-
stand better what is happening by relating them to linear Diophantine equations.
c 2019 by Bruce Ikenaga 7