You are on page 1of 12

Chapter 4

Mathematics of Coding

4.1 Remainders

Let us begin with a problem. Try to answer it yourself before reading the solution.

EXAMPLE 15. If today is Wednesday, what day will it be 100 days from now?

Solution. After every 7 days, it will be Wednesday again. Therefore, it will be Wednesday after 7, 14, 21,
28, . . . days. That is, if N is divisible by 7, then it will be Wednesday after N days.

What multiple of 7 is close to (but less than or equal to) 100? To find out, divide 100 ÷ 7 = 14 point
something. Thus, there are 14 sevens in 100. Now, multiply 14 × 7 = 98. This means that 98 is a multiple
of 7. Thus, 98 days from now, it will be Wednesday, and by counting, we can see that 100 days from now,
it will be Friday.

EXAMPLE 16. Give the remainder when 300 is divided by 9.

Solution. Using a similar procedure as above, let us find out how many 9s are there in 300. Divide
300 ÷ 9 = 33 point something. There are 33 nines in 200. Now multiply 3 × 9 = 297. This means that 297
has a remainder of 0 when divided by 9. By counting, 298 has a remainder of 1; 299 has a remainder of 2,
and 300 has a remainder of 3 when divided by 9.

Exercise 8. If today is Saturday, what day will it be 150 days from now?

Exercise 9. If today is Monday, what day was it 200 days ago?

33
34 CHAPTER 4. MATHEMATICS OF CODING

NAME:

1. Determine the remainders for each of the following division expressions.

Division Expression Remainder

79 ÷ 5

2134 ÷ 10

1836 ÷ 3

1021 ÷ 4

2097 ÷ 8

15811 ÷ 23

4866 ÷ 31

2. If today is Thursday, what day will it be 3,471 days from now?

3. If it is now 11:00 AM, what time will it be 298 hours from now?

4. If it is now 9:00 AM, what time was it 250 hours ago?

5. Give 5 numbers from 1000 to 1500 whose remainder when divided by 6 is 0.

6. Give 5 numbers from 1000 to 1500 whose remainder when divided by 6 is 2.


4.2. MODULAR ARITHMETIC 35

4.2 Modular Arithmetic

Solve this problem before reading the solution.

EXAMPLE 17. Give a three-digit number whose remainder when divided by 5 is 2.

Solution. All numbers that end in 0 or 5 are divisible by 5. That is, their remainders are 0 when divided
by 5. To obtain a remainder of 2, just add 2 to the multiples of 5. For example, 165 + 2 = 167 leaves a
remainder of 2 when divided by 5. Another is 280 + 2 = 282, which also leaves a remainder of 2 when
divided by 5.

To summarize, we say that 167 and 282 both leave a remainder of 2 when divided by 5. We have a special
name for this relationship, which we call modulo.

Modulo n
We say that two integers a and b are congruent modulo n if they leave the same remainder when
divided by n. In symbol we write
a ≡ b mod n.

Thus, from our previous example, we can say that

167 ≡ 282 mod 5.

Are two integers congruent mod n?


A quick way to check whether a ≡ b mod n is to compute a − b and check if the difference is
divisible by n.

Again using our previous example, compute 282 − 167 = 115, which is divisible by 5. Thus, 167 ≡ 282
mod 5.

Exercise 10. Determine whether each of the following statements are true.

1. 93 ≡ 10 mod 9 2. 17 ≡ 5 mod 12 3. 32 ≡ 53 mod 7

Exercise 11. Fill in the blanks. (There are more than 1 answer. Choose any.)

1. 87 ≡ mod 5 2. 93 ≡ mod 4 3. 161 ≡ mod 7


36 CHAPTER 4. MATHEMATICS OF CODING

NAME:

I. Determine whether the following congruence is true or false.

1. 47 ≡ 52 mod 5 5. 45 ≡ 39 mod 6

2. 181 ≡ 33 mod 8 6. 77 ≡ 105 mod 4

3. 122 ≡ 35 mod 11 7. 1149 ≡ 547 mod 10

4. 51 ≡ 63 mod 7 8. 115 ≡ 35 mod 20

II. Give FIVE different answers for each of the following.

1. 49 ≡ mod 10

2. 82 ≡ mod 9

3. 100 ≡ mod 21

III. Fill-in the blanks. Tare more than 1 answer. Choose any.

1. 46 ≡ 26 mod
2. 156 ≡ mod 15
3. 41 ≡ mod 6
4. 34 ≡ mod 24
5. ≡ 5 mod 17
6. ≡ 13 mod 23
4.3. OPERATIONS MODULO N 37

4.3 Operations Modulo n

EXAMPLE 18. It is now 10:00 PM. What time will it be after 6 hours?

Solution. Normally, we would add 10 + 6 = 14 but there is no such thing as 14:00 PM. The correct answer,
of course, is 4:00 AM. Two hours after 10:00 PM, the time is midnight, and the clock goes back to 0. Then
we count 4 hours more, to reach 4:00 AM.

The previous example shows the idea of performing operations modulo n. When the answer reaches n (or
a multiple of n), it is like the clock goes back to 0.

Operations modulo n
In operations modulo n, the answer is the remainder when the result of operation is divided by n.
Note that possible remainders are 0, 1, 2, . . . , n − 1.

EXAMPLE 19. What is 10 + 6 mod 12?

Solution. 10 + 6 = 16. But since the result modulo 12 must only be from 0, 1, . . . , 11, we need to get the
remainder. The remainder when 16 is divided by 12 is 4, so 10 + 6 = 4 mod 12.

Notation
To distinguish from our usual operations, we will use +n , −n , ×n for addition, subtraction, and
multiplication modulo n, respectively.

Exercise 12. Compute the following.

1. 2 +5 4 6. 2 +7 4
2. 128 +5 147 7. 128 +7 147
3. 2 ×5 4 8. 2 ×7 4
4. 461 ×5 783 9. 461 ×7 783
5. 1 −5 4 10. 1 −7 4
38 CHAPTER 4. MATHEMATICS OF CODING

NAME:

1. 43 +7 38 =

2. 47 ×3 3 =

3. 89 −10 174 =

4. 314 +2 326 =

5. 156 +5 324 =

Complete the following addition, subtraction, and multiplication tables, modulo 6. Note that the only
possible answers are 0, 1, 2, 3, 4, and 5.

+6 1 2 5 11 −6 1 2 5 11

2 3 2 3

4 4 2

6 5 6 5

7 3 7

9 9 2

×6 1 2 5 11 +6 3 2

2 3 5 0

4 2

6 5 1 5

7 3 6

9 1
4.4. PASCAL TRIANGLE MODULO N PROJECT 39

4.4 Pascal Triangle Modulo n Project

Pascal’s triangle is a triangular array of numbers where all numbers at the sides are 1 and other numbers
are the sum of the two numbers directly above it. Make two Pascal triangles modulo n, n = 2, 3, 4 or 5.

You can use the Pascal’s triangle grid provided or you can make/download your own grid with at least 16
rows. Make your Pascal triangle artistic, color the grids according to their numbers.

Pascal’s Triangle Modulo


40 CHAPTER 4. MATHEMATICS OF CODING

4.4.1 Credit Cards

Credit Card
A credit card is a form of cashless payment where purchases are paid by the bank first to be
reimbursed by the cardholder later.

Credit card numbers usually have 13 to 16 digits depending on credit card type as follows:

Card Type Prefix Number of Digits

MasterCard 51 to 55 16

Visa 4 13 or 16

American Express 34 or 37 15

The validity of credit card number is checked by doubling every other digit beginning with the 2nd to the
last digit and retaining the other digits. If the sum of the digits of these numbers is evenly divisible by 10,
the credit card is valid.

EXAMPLE 20. Check the validity of the credit card number 4132 1883 0214 6770. What card type is it?

Solution. We do the following steps in checking validity:

1. Box every other digit beginning with the 2nd to the last digit 4 1 3 2 1 8 8 3 0 2 1 4 6 7 7 0
2. Double the boxed digits and retain the non-highlighted ones 8162 28163 0224 127140
3. Take the sum of all digits in step 2.
8 + 1 + 6 + 2 + 2 + 8 + 1 + 6 + 3 + 0 + 2 + 2 + 4 + 1 + 2 + 7 + 1 + 4 + 0 = 60 ≡ 0 mod 10.
The card is valid because 60 is evenly divisible by 10. Also, the 1st digit of the card number (4)
indicates that it’s a Visa Card.

Exercise 13. Check the card number 3528 7004 2134 5998 for validity.

Exercise 14. Determine the last digit on the credit card number 4055 8711 7122 519[?].

Exercise 15. The number 51[?]6 2144 8118 4510 is shown on a credit card, but the third digit is not clear.
What is it?
4.4. PASCAL TRIANGLE MODULO N PROJECT 41

NAME:

Check the validity of the following credit cards and identify the card type.

1. 4417-5486-1785-6411

2. 3715-548731-84466

3. 5164-8295-1229-3674
42 CHAPTER 4. MATHEMATICS OF CODING

4.5 Cryptology

Secret messages have been sent since the ancient times, especially in military affairs. Nowadays, secrecy
is necessary in financial dealings with the advent of electronic banking and online transactions. Most of
these secrecy systems are based on modular arithmetic.

Cryptology
Cryptology is the discipline devoted to secrecy systems. It is the study of making and breaking
secret codes.

4.5.1 Pigpen Cipher

The method for altering message to secret form is called a cipher. A pigpen cipher is a simple geometric
substitution cipher that makes use of a grid system as shown below.

In pigpen cipher letters are substituted by the part of the tic-tac-toe grids that they are in. Using this
method MATH will be encrypted as:

Exercise 16. Decode

Exercise 17. Decode


4.5. CRYPTOLOGY 43

4.5.2 Character Cipher

Character Cipher
A character cipher is a letter substitution cipher.

If we take the English alphabet as our standard, the letters from A to Z are given numerical equivalents
from 0 to 25 as follows:

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

0 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

A cipher that was used by Julius Caesar is based on substitution in which each letter is replaced by the
letter 3 further down the alphabet and last 3. If we let P be the numeric equivalent of the letter in the
plaintext and C in the ciphertext, the transformation will correspond to the modular equation

C ≡P +3 mod 26.

Exercise 18. Using the Caesar cipher to encrypt the plaintext MATH IS FUN. You may use the table
below to help you.

Plaintext M A T H F I S F F U N

P 12 5

C = P + 3 mod 26 15 8

Ciphertext P I

Exercise 19. The message VWXGB KDUG was encrypted using the Caesar cipher. What was the original
message?

Ciphertext V W X G B F K D U G

C 21

P = C − 3 mod 26 18

Plaintext S
44 CHAPTER 4. MATHEMATICS OF CODING

NAME:

1. Decode

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

0 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

2. Write SEND MONEY using the encrypting congruence C ≡ 5P + 4 mod 26.

Plaintext S E N D F M O N E Y

P 18

C = 5P + 4 mod 26 16

Ciphertext Q

3. Decode UGXTCS XC CTTS that has been transformed using C ≡ P + 15 mod 26.

Ciphertext U G X T C S F X C F C T T S

C 21

P = mod 26 18

Plaintext F

4. Decode the altered message WH GHM VHIR that was encrypted using the given congruence C ≡ P −7
mod 26.

W H F G H M F V H I R

5. Decode the altered message CGUACHUNCIH LOFYM NBY QILFX that was encrypted using the
given congruence C ≡ P + 20 mod 26.

C G U A C H U N C I H s L O F Y M s N B Y s Q I L F X

You might also like