Modular arithmetic is remainder maths. When you say “17 mod 5 = 2”, you mean that 17 divided by 5 leaves a remainder of 2. It is the maths behind clock times, weekly schedules, and computer codes, and it only needs division you already know.
The basic idea
The expression “a mod n” means the remainder when a is divided by n. The number n is called the modulus. For example, 17 mod 5 = 2 because 17 = 3 x 5 + 2. Similarly, 20 mod 5 = 0, because 20 divides evenly. When the remainder is 0, we say a is a multiple of n.
Clock arithmetic is the everyday version. A clock uses mod 12: 10 o’clock plus 5 hours is 3 o’clock, because 15 mod 12 = 3. Days of the week use mod 7: if today is Monday, then 10 days from now is Thursday, because 10 mod 7 = 3 days past Monday.
Addition and multiplication
You can add and multiply before or after taking remainders, and the answer is the same. That shortcut saves a lot of work. Example: (7 + 8) mod 6. Directly, 15 mod 6 = 3. By the shortcut, 7 mod 6 = 1 and 8 mod 6 = 2, and 1 + 2 = 3. Same answer.
Multiplication works the same way. (7 x 8) mod 6: directly, 56 mod 6 = 2, since 56 = 9 x 6 + 2. By the shortcut, 7 mod 6 = 1 and 8 mod 6 = 2, and 1 x 2 = 2. Same answer. This is why modular arithmetic is so useful for huge numbers: you can reduce each piece first and keep the numbers small.
Negative numbers
Negative numbers confuse beginners, but the rule is simple: keep adding the modulus until the result is between 0 and n – 1. For -3 mod 5, add 5 once: -3 + 5 = 2, so -3 mod 5 = 2. For -8 mod 5, add 5 twice: -8 + 10 = 2, so -8 mod 5 = 2 as well.
Note that some programming languages give -3 for -3 mod 5 instead of 2. That is a language quirk, not the mathematical answer. In mathematics, remainders are always taken in the range 0 to n – 1.
A full worked example
Find (23 x 17 + 41) mod 9. Reduce each number first: 23 mod 9 = 5, 17 mod 9 = 8, 41 mod 9 = 5. Then 5 x 8 + 5 = 45. Finally 45 mod 9 = 0. So the answer is 0. Check directly if you like: 23 x 17 + 41 = 391 + 41 = 432, and 432 / 9 = 48 exactly, so the remainder is 0. Both routes agree.
Modular inverses and everyday uses
A modular inverse answers: what number, multiplied by a, gives 1 mod n? For example, 5 is the inverse of 3 mod 7, because 3 x 5 = 15, and 15 mod 7 = 1. Inverses exist only when a and n share no common factor greater than 1, which is why prime moduli are popular in cryptography.
Everyday uses are everywhere: check digits on ISBNs and ID numbers use modular arithmetic to catch typos. Calendar maths uses mod 7 for weekdays. Computer graphics wraps screen coordinates with mod operations. And cryptography, from secure messaging to card payments, relies on modular arithmetic with very large numbers.
Try it faster with the tool
You can follow every step above by hand, but the free Modular Arithmetic Calculator does the arithmetic for you. Enter your numbers and it gives the answer right away, so you can check your hand calculation or skip straight to the result.
Quick tips
- a mod n is the remainder when a is divided by n.
- Reduce each number first, then add or multiply.
- For negatives, add the modulus until the result is 0 or more.
