1. An introduction
When we divide two integers we will have an equation that looks like the
following:
A/B = Q remainder R
A
is the dividend
B
is the divisor
Q
is the quotient
R
is the remainder
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 congruent to R
. Where B
is referred to as the modulus.
For example:
13/5 = 2 remainder 3
13 mod 5 = 3If we have
A mod B
and we increase
A
by a
multiple of B
, we will end up in A mod B=(A+K⋅B) mod B
for any integer K.
2. Congruence Modulo
You may see an expression like:
A≡B(mod C)equivalent
This says that
A
is congruent to B
modulo C
.
Let's imagine we were calculating mod 5 for all of the integers:
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, 26would go in the slicelabeled 1, because 26 mod 5=1.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)
.
Examining the expression closer:
1.
≡
is the symbol for congruence, which means the values A
and B
are in
the same equivalence class.
2.
(mod C)
tells us what operation we applied to A
and B
.
3.when we have both of these, we call “
≡
” congruence modulo C
.
e.g.
26≡11 (mod 5)
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 multipleof
C
.
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)
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×−2=−10)
13=23+K⋅5
. We can satisfy this with K=−2
:
13=23+(−2)×5
Congruence Modulo is an Equivalence Relation
The Quotient Remainder Theorem
When we want to prove some properties about modular arithmetic we oftenmake 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 < BWe 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
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
Modular addition andsubtraction
(A + B) mod C = (A mod C + B mod C) mod CProof:
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
(A + B) mod C = ( C *(Q1 + Q2) + R1 + R2) mod C = (R1 + R2) mod C
(A mod C + B mod C) mod C = (R1 + R2) mod c
A very similar proof holds for modular subtraction
(A - B) mod C = (A mod C - B mod C) mod C
Let's explore the multiplication property of modular arithmetic:
(A * B) mod C = (A mod C * B mod C) mod C
Finally, let's explore the exponentiation property:
A^B mod C = ( (A mod C)^B ) mod C
Often we want to calculate A^B mod C for large values of B .
Unfortunately, A^B becomes very large for even modest sized values for B.
These huge values cause our calculators and computers to
return overflowerrors.Even if they didn't, it would take a long time
to find the mod of these hugenumbers directly.
Suppose we want to calculate 2^90 mod 13, but we have a calculator that
can't hold any numbers larger than 2^50.
Here is a simple divide and conquer strategy:
Step 1: Split A^B into smaller parts using exponent rules
2^90 = 2^50 * 2^40
Step 2: Calculate the mod C for each part
2^50 mod 13 = 1125899906842624 mod 13 = 4
2^40 mod 13 = 1099511627776 mod 13 = 3
Step 3: Use modular multiplication properties to combine the parts
2^90 mod 13 = (2^50 * 2^40) mod 13
2^90 mod 13 = (2^50 mod 13 * 2^40 mod 13) mod 13
2^90 mod 13 = ( 4 * 3 ) mod 13
2^90 mod 13 = 12 mod 13 = 12
https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic