Lecture 10 Modular Arithmetic
Lecture 10 Modular Arithmetic
Modulo n
𝑎−𝑏
Two integers a and b are said to be congruent modulo n, where n is a natural number, if 𝑛 is
an integer. In this case, we write a ≡b mod n. The number n is called the modulus. The
statement a≡b mod n is called a congruence.
29−8 21
Find = = 7. 𝐵𝑒𝑐𝑎𝑢𝑠𝑒 7 𝑖𝑠 𝑎𝑛 𝑖𝑛𝑡𝑒𝑔𝑒𝑟, 29 ≡ 8 𝑚𝑜𝑑 3 𝑖𝑠 𝑎 𝑡𝑟𝑢𝑒 𝑐𝑜𝑛𝑔𝑟𝑢𝑒𝑛𝑐𝑒.
3 3
b. 15 ≡ 4 mod 6
15−4 11 11
Find 6 = 6 . 𝐵𝑒𝑐𝑎𝑢𝑠𝑒 6 𝑖𝑠 𝑛𝑜𝑡 𝑎𝑛 𝑖𝑛𝑡𝑒𝑔𝑒𝑟, 15 ≡ 4 𝑚𝑜𝑑 6 𝑖𝑠 𝑛𝑜𝑡 𝑎 𝑡𝑟𝑢𝑒 𝑐𝑜𝑛𝑔𝑟𝑢𝑒𝑛𝑐𝑒.
Example 2: a Day of the Week
July 4, 2010, was a Sunday. What day of the week is July 4, 2015?
Solution: There are 5 years between the two dates. Each year has
365 days except 2012, which has one extra day because it is a leap
year. So the total number of days between the two dates is 5 x 365
+ 1 = 1826. Because 1826 ÷ 7 = 260 remainder 6, 1826 ≡ 6 mod 7.
Any multiple of 7 days past a given day will be the same day of the
week. So the day of the week 1826 days after July 4, 2010, will be
the same as the day 6 days after July 4, 2010. Thus July 4, 2015,
will be a Saturday.
Arithmetic Operations
Arithmetic modulo n, where n is a natural number,
uses a variation of the standard rules of arithmetic
we have used before. Perform the arithmetic
operation and then divide by the modulus. The
answer is the remainder. Thus the result of an
arithmetic operation mod n is always a whole
number less than n.
Example 3: Addition modulo n
Evaluate: 23 + 38 𝑚𝑜𝑑 12
Solution
61 ÷ 12 = 5, 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 𝑖𝑠 1
The answer is 1.
Example 4: Subtraction modulo n
Evaluate each of the following:
a. (33 – 16) mod 6 b. (14 – 27) mod 5
Solution
a. Subtract 33 – 16 = 17. The result is positive. Divide the
difference by the modulus, 6. The answer is the remainder
17 ÷ 6 = 2, 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 𝑖𝑠 5
Solution
Find the product 15 * 23 and then divide by the modulus, 11. The
answer is the remainder.
345 ÷ 11 = 31, remainder is 4.
The solutions between 0 and 9 are 1 and 6; the remaining solutions are determined by
repeatedly adding the modulus, 10, to these solutions. The solutions are 1, 6, 11, 16,
21, 26, …
Additive and Multiplicative Inverse in Modular Arithmetic
Recall that if the sum of two numbers is 0, then the numbers are additive
inverses of each other. For instance, 8 + (-8) = 0, so 8 is the additive inverse of -8
and -8 is the additive inverse of 8.
The same concept applies in modular arithmetic. For example, (3 + 5) ≡ 0
mod 8. Thus, in mod 8 arithmetic, 3 is the additive inverse of 5, and 5 is the
additive inverse of 3. Here we consider only those whole numbers smaller than
the modulus. Note that 3 + 5 = 8; that is, the sum of a number and its additive
inverse equals the modulus. Using this fact, we can easily find the additive
inverse of a number for any modulus. For instance, in mod 11 arithmetic, the
additive inverse of 5 is 6 because 5 +6 = 11.
2𝑥 ≡ 1 𝑚𝑜𝑑 7
2(1) ≠ 1 𝑚𝑜𝑑 7 Try x = 1
2(2) ≠ 1 𝑚𝑜𝑑 7 Try x = 2
2(3) ≠ 1 𝑚𝑜𝑑 7 Try x = 3
2(4) ≠ 1 𝑚𝑜𝑑 7 Try x = 4
The first three digits of an ISBN are 978, the next digit indicates the country in
which the publisher is incorporated (0, and sometimes 1, for books written in
English), the next two to seven digits indicate the publisher, the next group of
digits indicates the title of the book, and the last digit (the 13th one) is called a
check digit.
If we label the first digit of an ISBN 𝑑1 , the second digit 𝑑2 , and so on to the 13th
digit 𝑑13 , then the check digit is chosen to satisfy the following congruence.
Determine the ISBN check digit for the book The Equation that
Couldn’t Be Solved by Mario Livio. The first 12 digits of the ISBN are
978-0-7432-5820-?
Solution:
𝑑13
≡ 10
− ሾ9 + 3 7 + 8 + 3 0 + 7 + 3 4 + 3 + 3 2 + 5 + 3 8 + 2
UPC (Universal Product Code)
The UPC is a 12-digit number that satisfies a congruence equation that is similar
to the one for ISBNs. The last digit is the check digit. If we label the 12 digits of
the UPC as 𝒅𝟏 , 𝒅𝟐 ,…, 𝒅𝟏𝟐 , we can write a formula for the UPC check digit 𝒅𝟏𝟐 .
𝑑12 = 10 − (3𝑑1 +𝑑2 +3𝑑3 + 𝑑4 + 3𝑑5 + 𝑑6 + 3𝑑7 + 𝑑8 + 3𝑑9 + 𝑑10 + 3𝑑11 ) mod 10
If 𝑑13 = 10, then the check digit is 0.
Example 11
Find the check digit for the DVD release of the film Alice in Wonderland. The
first 11 digits are 7-86936-79798-?
Solution:
𝑑12 = 10 − ሾ3 7 + 8 + 3 6 + 9 + 3 3 + 6 + 3 7 + 9 + 3 7 + 9 + 3 8 ] mod 10
𝑑12 = 10 − 155mod 10 = 10 − 5 = 5
Companies that issue credit cards also use modular arithmetic to determine
whether a credit card number is valid. This is especially important in e-commerce,
where credit card information is frequently sent over the Internet. The primary
coding method is based on the Luhn algorithm, which uses mod 10 arithmetic.
Credit card numbers are normally 13 to 16 digits long. The first one to four digits
are used to identify the card issuer. The table below shows the identification
prefixes used by four popular card issuers.
Card issuer Prefix Number of digits
MasterCard 51 to 55 16
Visa 4 13 or 16
American Express 34 or 37 15
Discover 6011 16
Credit Card Numbers
The Luhn algorithm, used to determine whether a credit card number is valid, is
calculated as follows: Beginning with the next-to-last digit (the last digit is the check
digit) and reading from right to left, double every other digit. If a digit becomes a
two-digit number after being doubled, treat the number as two individual digits. Now
find the sum of the new list of digits; the final sum must equal 0 mod 10.
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 0
If the encrypting code is to shift each letter of the plaintext message m
positions, then the corresponding letter in the ciphertext message is given by 𝑐 ≡
𝑝 + 𝑚 𝑚𝑜𝑑 26, where p is the numerical equivalent of the plaintext letter and c
is the numerical equivalent of the ciphertext letter. The letter Z is coded as 0
because 26 ≡0 mod 26.
Each letter in Lord Byron’s poem was shifted 22 positions (m = 22) to the right.
To code the plaintext letter S in the word SHE, we use the congruence 𝑐 ≡
𝑝 + 𝑚 𝑚𝑜𝑑 26.
𝑐 ≡ 𝑝 + 𝑚 𝑚𝑜𝑑 26
𝑐 ≡ 19 + 22 𝑚𝑜𝑑 26
𝑐 ≡ 41 𝑚𝑜𝑑 26
𝑐 = 15
The 15th letter is O. Thus S is coded as O.
Once plaintext has been converted to ciphertext, there must be a
method by which the person receiving the message can return the
message to plaintext. For the cyclical code, the congruence is 𝑝 ≡
𝑐 + 𝑛 𝑚𝑜𝑑 26, where p and c are defined as before and n = 26 – m.
The letter O in ciphertext is decoded below using the congruence 𝑝 ≡
𝑐 + 𝑛 𝑚𝑜𝑑 26.
𝑝 ≡ 𝑐 + 𝑛 𝑚𝑜𝑑 26
𝑝 ≡ 15 + 4 𝑚𝑜𝑑 26
𝑝 ≡ 19 𝑚𝑜𝑑 26
𝑝 = 19
The 19th letter is S. Thus O is decoded as S.
Example 13 Write Messages Using Cyclical Coding