Modular Arithmetic: The Math of Remainders

Have you ever wondered how to calculate the day of the week for any given date? Or how to encrypt and decrypt messages using secret codes? Or how to create beautiful patterns and designs using numbers and shapes?

If so, then you might be interested in learning about modular arithmetic, a system of arithmetic that deals with the remainder of division.

In this blog, I will explain what modular arithmetic is, how it works, and why it is useful for many applications and purposes.

What is modular arithmetic?
Modular arithmetic is a system of arithmetic for integers, which considers the remainder. In modular arithmetic, numbers “wrap around” upon reaching a given fixed quantity (this given quantity is known as the modulus) to leave a remainder.

For example, when you divide 7 by 3, you get a quotient of 2 and a remainder of 1. In modular arithmetic, we write this as 7 mod 3 = 1, which means that 7 leaves a remainder of 1 when divided by 3.

Modular arithmetic is sometimes referred to as modulus arithmetic or clock arithmetic, because it is similar to how we measure time using a clock. For example, if it is 9 o’clock now, then 4 hours later it will be 1 o’clock, not 13 o’clock. This is because we use a 12-hour clock, which has a modulus of 12. In other words, we add or subtract multiples of 12 to get a number between 0 and 11. This is equivalent to finding the remainder when dividing by 12.

How does modular arithmetic work?
Modular arithmetic has some basic properties that are similar to normal arithmetic, such as:

  • (a + b) mod n = (a mod n + b mod n) mod n
  • (a – b) mod n = (a mod n – b mod n) mod n
  • (a x b) mod n = (a mod n x b mod n) mod n

These properties mean that we can perform addition, subtraction, and multiplication in modular arithmetic by first finding the remainder of each operand when divided by the modulus, then performing the operation on the remainders, and then finding the remainder of the result when divided by the modulus again.

For example, if we want to calculate (15 + 17) mod 7, we can do it as follows:

  • Find the remainder of 15 when divided by 7: 15 mod 7 = 1
  • Find the remainder of 17 when divided by 7: 17 mod 7 = 3
  • Add the remainders: (15 mod 7 + 17 mod 7) = (1 + 3) = 4
  • Find the remainder of the sum when divided by 7: (15 + 17) mod 7 = (1 + 3) mod 7 = 4 mod 7 = 4

Therefore, (15 + 17) mod 7 = 4.

However, modular arithmetic also has some differences from normal arithmetic, such as:

  • Division is not always possible in modular arithmetic. For example, there is no integer x such that 2x mod 6 = 1.
  • Modular arithmetic is not commutative for exponentiation. For example, (2^3)^4 mod 5 = 1, but 2^(3^4) mod 5 = 2.
  • Modular arithmetic can have different solutions for the same equation. For example, x^2 mod 5 = 1 has two solutions: x = 1 and x = 4.

These differences mean that we have to be careful when performing division, exponentiation, and solving equations in modular arithmetic. We may need to use some special techniques or tools to find the correct answers.

Why is modular arithmetic useful?
Modular arithmetic is useful for many applications and purposes. One of them is cryptography. Modular arithmetic is used to create and break secret codes and ciphers that are used to encrypt and decrypt messages. For example, one of the most famous ciphers in history is the Caesar cipher, which shifts each letter in a message by a fixed number of positions in the alphabet. This cipher can be seen as a form of modular arithmetic with modulus