An Introduction to Modular Math
When we divide two integers we will have an equation that looks like the following:
𝐴
= 𝑄 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 𝑅
𝐵
Sometimes, we are only interested in what the remainder is when we divide A by B .
For these cases there is an operator called the modulo operator (abbreviated as mod).
Using the same A, B, Q, and R as above, we would have: A mod B = R
We would say this as A modulo B is equal to R. Where B is referred to as the modulus.
13
= 2 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 3
5
13 mod 5 = 3
Visualize modulus with clocks
Observe what happens when we increment numbers by one and then divide them
by 3.
0
= 0 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 0
3
1
= 0 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 1
3
2
= 0 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 2
3
3
= 1 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 0
3
4
= 1 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 1
3
5
= 1 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 2
3
6
= 2 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 0
3
The remainders start at 0 and increases by 1 each time, until the number reaches one
less than the number we are dividing by. After that, the sequence repeats.
By noticing this, we can visualize the modulo operator by using circles.
We write 0 at the top of a circle and continuing clockwise writing integers 1, 2, ... up to
one less than the modulus.
For example, a clock with the 12 replaced by a 0 would be the circle for a modulus of
12.
To find the result of
A mod B we can follow these steps:
Construct this clock for size B
Start at 0 and move around the clock W steps
Wherever we land is our solution.
(If the number is positive we step clockwise, if it’s negative we step counter-clockwise.)
Examples
8 mod 4 =
With a modulus of 4 we make a clock with numbers 0, 1, 2, 3.
We start at 0 and go through 8 numbers in a clockwise sequence 1, 2, 3, 0, 1, 2, 3, 0.
We ended up at 0 so 8 mod 4 = 0
7 mod 2 = ?
With a modulus of 2 we make a clock with numbers 0, 1.
We start at 0 and go through 7 numbers in a clockwise sequence 1, 0, 1, 0,
1, 0, 1.
-5 mod 3
With a modulus of 3 we make a clock with numbers 0, 1, 2.
We start at 0 and go through 5 numbers in counter-clockwise sequence (5 is negative) 2,
1, 0, 2, 1.
Conclusion
If we have mod B and we increase A by a multiple of B, we will end up in
the same spot, i.e.
A mod B = (A + K. B) mod B for any integer K.
For example:
3 mod 10 = 3
13 mod 10 = 3
23 mod 10 = 3
33 mod 10 = 3
Exercise
What is 9 mod 6?
What remainder do we get when we divide 9 by 6?
When we divide a dividend, A, by a divisor, B, we get a quotient, Q, with a remainder, R.
A/B=Q with a remainder of where 0<=R<B
If we can find the remainder, R, in the expressions above then we can calculate A mod B
since:
A mod B=R
Our dividend, A= 9
Our divisor, B= 6
If we can calculate
Q, then we can calculate R
We can rewrite our expression as:
A=Q . B+R
Or alternatively as:
A/B = Q + R/B
This shows us how to find Q.
0<= R < B we have:
0<= R/B < 1
If we use decimal division to divide
A by B, then Q will be ⌊A/B⌋
⌊x⌋ means the next smallest integer less than or equal to x
e.g.
⌊2⌋ =2; ⌊7.25⌋ =7; ⌊-5.1⌋ = -6
Q = ⌊9/6⌋ = 1
A = Q. B + R
9 = 1.6 + R
R=3
What is -49 mod 5?
Q = ⌊-49/5⌋ = -10
-49 = -10 . 5 + R
R=1
What is 13 mod 1?
Q = ⌊13/1⌋ = 13
R=0
What is -4 mod 9?
Q = ⌊-4/9⌋ = -0, 44 = -1
R= 5
What is 14 mod 2?
R=0
What is 7 mod 6?
R=1
What is -29 mod 3?
R=1
We will discuss the meaning of congruence modulo by performing a thought
experiment with the regular modulo operator.
Let’s imagine we were calculating mod 5 for all of the integers:
Suppose we labelled 5 slices 0, 1, 2, 3, 4. Then, for each of the
integers, we put it into a slice that matched the value of the integer
mod 5.
Think of these slices as buckets, which hold a set of numbers. For
example, 26 would go in the slice labelled 1, because 26 mod 5 = 1
Above is a figure that shows some integers that we would find in each
of the slices.
It would be useful to have a way of expressing that numbers belonged
in the same slice. (Notice 26 is in the same slice as 1, 6, 11, 16, 21 in
above example).
A common way of expressing that two values are in the same
slice, is to say they are in the same equivalence class.
The way we express this mathematically for mod C is: A ≡ B
(mod C)
For example:
26 ≡ 11 mod 5
26 mod 5 = 1 so it is in the equivalence class for 1,
11 mod 5 = 1 so it is in the equivalence class for 1 as well
Note, that this is different from A mod C:
26 ≠ 11 mod 5
Insights into Congruence Modulo
We can gain some further insight behind what congruence
modulo means by performing the same thought experiment using
a positive integer C .
First, we would label C slices 0, 1, 2, ..., C - 2, C – 1.
Then, for each of the integers, we would put it into a slice that
matched the value of the integer mod C.
Below is a figure that shows some representative values that we
would find in each of the slices
If we looked at the bucket labelled 0 we would find:
..., -3C, -2C,C, 0, C, 2C, 3C, ...
If we looked at the bucket labelled 1 we would find:
. . ., 1-3C, 1-2C, 1-C, 1, 1+C, 1+ 2C, 1+3C, ...
If we looked at the bucket labelled 2 we would find:
..., 2-3C, 2-2C, 2-C, 2, 2+C, 2+2C, 2+3C,...
If we looked at the bucket for C - 1 we would find:
. .., -2C -1, -C-1, -1, C-1, 2C-1, 3C-1,...
From this experiment we can make a key observation:
The values in each of the slices are equal to the label on the slice plus or minus some
multiple of C
This means the difference between any two values in a slice is some multiple of
C.
This observation can help us understand equivalent statements and equivalence classes
next.
Given that x ≡ -47 mod 14 then x mod 14 = -47 mod 14
-13
-6
23
47
-47 Mod 14 = 9
Valid solutions for x must be 9 plus or minus some integer multiple of 14
-13 mod 14 = 1
-6 mod 14 = 8
7 mod 14 = 7
23 mod 14 = 9
47 mod 14 = 5
23 is the valid solution
23 = 14 x 1 + 9
Equivalent Statements
Before proceeding it’s important to remember the following statements
are equivalent
A≡B (mod C)
A mod C = B mod C
C | (A-B) (The symbol | means divides or is a factor of)
A = B + K. C (where K is some integer)
This lets us move back and forth between different forms of expressing the same idea.
For example the following are equivalent:
13≡ 23(mod 5)
13 mod 5 = 23 mod 5
5 | (13-23) , 5|-10 which is true since 5 x -2 = -10
13 = 23 + K. 5
13 = 23 + (-2) . 5
Congruence Modulo is an Equivalence Relation
Convince yourself that the slices used in the previous example have the following
properties:
Every pair of values in a slice are related to each other
We will never find a value in more than one slice (slices are mutually disjoint)
If we combine all the slices together they would form a pie containing all of the values
A pie with slices that have these properties has an equivalence relation.
An equivalence relation defines how we can cut up our pie (how we partition our set of
values) into slices (equivalence classes).
In general, equivalence relations must have these properties:
The pie: A collection of all the values we are interested in
A slice of pie: An equivalence class
How we cut the pie into slices: equivalence relation
Specifically, for our previous example:
The pie: The collection of all integers
A slice of pie labelled B: Equivalence class where all the values mod C=B
How we cut the pie into slices: Using the congruence modulo C relation,≡ (mod C)
This is why we say that Congruence modulo C is an equivalence relation. It
partitions the integers into C different equivalence classes.
Why do we care that congruence modulo C is an equivalence
relation ?
Knowing that congruence modulo C is an equivalence relation lets us
know about some properties that it must have.
Equivalence relations are relations that have the following properties:
1. They are reflexive: A is related
They are symmetric: if A is related to B, then B is related to A
They are transitive: if A is related to B and B is related to C then A is related to C
Since congruence modulo is an equivalence relation for (mod C). This means:
A ≡ A (mod C)
If A ≡ B (mod C) then B ≡ A mod C
If A ≡ B (mod C) and B ≡ D (mod C) then A ≡ D (mod C)
Example
Let's apply these properties to a concrete example using mod 5:
3 ≡ 3 (mod 5) (reflexive property)
If 3 ≡ 8 (mod 5) then 8 ≡ 3 (mod 5) (symmetry property)
If 3 ≡ 8 (mod 5) and if 8 ≡ 18 (mod 5) then 3 ≡ 18 (modb3)
The quotient remainder theorem
When we want to prove some properties about modular arithmetic we
often make use of the quotient remainder theorem.
It is a simple idea that comes directly from long division.
The quotient remainder theorem says:
Given any integer A, and a positive integer B, there exist unique integers Q
and R such that
A= B * Q + R where 0 ≤ R < B
We can see that this comes directly from long division. When we divide A by
B in long division, Q is the quotient and R is the remainder.
If we can write a number in this form then A mod B = R
Examples
A = 7, B = 2
7=2*3+1
7 mod 2 = 1
A = 8, B = 4
8=4*2+0
8 mod 4 = 0
A = 13, B = 5
13 = 5 * 2 + 3
13 mod 5 = 3
A = -16, B = 26
-16 = 26 * -1 + 10
-16 mod 26 = 10
Let’s explore the addition property of modular arithmetic:
(A + B) mod C = (A mod C + B mod C) mod C
Example:
Let A=14, B=17, C=5
Let’s verify: (A + B) mod C = (A mod C + B mod C) mod C
LHS = Left Hand Side of the Equation
RHS = Right Hand Side of the Equation
LHS = (A + B) mod C
LHS = (14 + 17) mod 5
LHS = 31 mod 5
LHS = 1
RHS = (A mod C + B mod C) mod C
RHS = (14 mod 5 + 17 mod 5) mod 5
RHS = (4 + 2) mod 5
RHS = 1
Intuition Behind Modular Addition
Observe the figure below. If we want to calculate 12+9 mod 7 we can easily go around
the modular circle for a sequence of 12+9 steps clockwise (as shown in the bottom left
circle).
We can take a shortcut by observing that every 7 steps we end up in the same position
on the modular circle. These complete loops around the modular circle don’t contribute
to our final position. We ignore these complete loops around the circle by calculating
each number mod 7 (as shown in the two upper modular circles). This will give us the
number of clockwise steps, relative to 0, that contributed to each of their final positions
around the modular circle.
Now, we only have to go around the circle clockwise the total of the number of steps that
contributed to each of numbers final position (as shown in the bottom right modular
circle). This method applies, in general, to any two integers and any modular circle.
Proof for Modular Addition
We will prove that (A + B) mod C = (A mod C + B mod C) mod C
We must show that LHS=RHS
From the quotient remainder theorem we can write A and B as:
A = C * Q1 + R1 where 0 ≤ R1 < C and Q1 is some integer. A mod C =
R1
B = C * Q2 + R2 where 0 ≤ R2 < C and Q2 is some integer. B mod C =
R2
(A + B) = C * (Q1 + Q2) + R1+R2
LHS = (A + B) mod C
LHS = (C * (Q1 + Q2) + R1+ R2) mod C
We can eliminate the multiples of C when we take the mod C
LHS = (R1 + R2) mod C
RHS = (A mod C + B mod C) mod C
RHS = (R1 + R2) mod C
LHS=RHS= (R1 + R2) mod C
Modular Subtraction
A very similar proof holds for modular subtraction
(A - B) mod C = (A mod C - B mod C) mod C
(918-236) mod 20=Y
Solve for Y
(918-236) mod 20= 918 mod 20 + (-236(mod 20)) mod 20
(918-236) mod 20= (18+4)mod 20
(918-236) mod 20= (22)mod 20
(918-236) mod 20= 2
Y=2
Given
A mod 8 = 3
(A+19) mod 8 = Y
Solve for Y
(3+3) mod 8
6 mod 8
Y=6
Given
A mod 13 = 11
(64+A) mod 13 = 11
( 64 + A ) mod 13 =( 64 mod 13 + A mod 13 ) mod 13
( 64 + A ) mod 13 = (12 + 11 ) mod 13
( 64 + A ) mod 13 = 23 mod 13
( 64 + A ) mod 13 = 10
Which of the following statements are equivalent to
724 mod 17
A: (72 mod 17 + 4 mod 17) mod 17]
B: (700 mod 17 + 20 mod 17 + 4 mod 17) mod 17
C: (7 mod 17 + 2 mod 17 + 4 mod 17) mod 17
D: (24 mod 17 + 700 mod 17) mod 17
(417+93) mod 4 = Y
Solve for Y
(1+1) mod 4
Y=2
Which of the following statements are equivalent to: 111 mod 10?
A: (110 mod 10 + 1 mod 10) mod 10
B: (1 mod 10 + 1 mod 10 + 1 mod 10} mod 10
C: (100 mod 10 + 10 mod 10 + 1 mod 10) 1 mod 10
D: (11 mod 10 + 100 mod 10) mod 10
Examine the statement one by one
A C D are correct
111 mod 10 = (101 + 10) mod 10
(372+35) mod 18 = Y
Solve for Y
(12+17) mod 18
29 mod 18
Y = 11
A mod 47 = 43
B mod 47 = 46
(A+B) mod 47 = Y
Solve for Y
89 mod 47 = 2
Which of the following statements are equivalent to
1594 mod 6
A: (1800 mod 6 + (-206 mod 6) mod 6
B: (1200 mod 6 + 300 mod 6 + 94 mod 6) mod 6
C: (1500 mod 6 + 90 mod 6 + 4 mod 6) mod 6
D: (15 mod 6 + 94 mod 6) mod 6
A, B and C are correct
(121+34) mod 11= Y
Solve for Y
(0+1) mod 11
1 mod 11
Y=1
A mod 9 = 7
(A-21) mod 9 = Y
Solve for Y
Y=4
Given
A mod 12 = 3
A + 41 mod 12 = Y
Solve for Y
(A + 41) mod 12 = (A mod 12 + 41 mod 12) mod 12
(A + 41 ) mod 12 = (3+5) mod 12
(A + 41) mod 12 = 8
(894-573) mod 17
A. 6 mod 7
B. (2+3) mod 7
C. (7-5) mod 7
D. 5
E. 2 mod 7
F. (5+1) mod 7
A and F are the answers
Which of the following expressions are equivalent to: (107+22) mod 10
(10+2) mod 10
9
(7.2) mod 10
(7+2) mod 10
9 mod 10
(7-2) mod 10
(-924-156) mod 4 = Y
Solve for Y
0+0 mod 4
A mod 7 = 5
B mod 7 = 6
(A+B) mod 7 = Y
Solve for Y
11 mod 7
Y=4
Choose all answers that apply:
Which of the following expressions are equivalent to:
(89 + 13) mod 6
Choose all answers that apply:
(Choice A)
(5 + 1 ) mod 6
(Choice B)
4
(Choice C)
3 mod 6
(Choice D)
6 mod 6
(Choice E)
\[ (5 – 2 ) \text{ mod } 6 \]
(5 – 2 ) mod 6
(Choice F)
(1 + 3 ) mod 6