What the definition says
- a mod n = a − n · ⌊a/n⌋
for a whole number a and a whole number n ≥ 1. ⌊a/n⌋ is the largest whole number not greater than a/n, that is a/n rounded down. The result is the remainder when a is divided by n, and it lies between 0 and n − 1.
Two whole numbers a and b are congruent modulo n, written a ≡ b (mod n), when they leave the same remainder:
- a ≡ b (mod n) means a mod n = b mod n, which is the same as saying that n divides a − b.
So a congruence is a statement about two numbers, while a mod n is a number.
Worked examples
A positive number. 47 mod 5: 47/5 = 9.4 and ⌊9.4⌋ = 9, so 47 mod 5 = 47 − 5 · 9 = 2. Check: 47 = 5 · 9 + 2.
A negative number. −7 mod 3: −7/3 ≈ −2.33. Rounded down, that is −3, not −2. So −7 mod 3 = −7 − 3 · (−3) = −7 + 9 = 2. Check: −7 = 3 · (−3) + 2, and 2 lies between 0 and 2.
A congruence. 38 ≡ 14 (mod 12), because 38 mod 12 = 2 and 14 mod 12 = 2. The difference 38 − 14 = 24 is divisible by 12. This is clock arithmetic: 38 hours after midnight, a 12-hour clock shows the same as 14 hours after midnight, 2 o’clock.
The negative remainder
Negative numbers are where most mistakes happen. Many calculators and programming languages divide and cut off the decimals, so −7/3 becomes −2 instead of −3. The remainder then comes out as −7 − 3 · (−2) = −1 instead of 2.
| a | n | a/n | Cut off | Rounded down | Wrong remainder | a mod n |
|---|---|---|---|---|---|---|
| 47 | 5 | 9.4 | 9 | 9 | 2 | 2 |
| −7 | 3 | −2.33 | −2 | −3 | −1 | 2 |
| −23 | 6 | −3.83 | −3 | −4 | −5 | 1 |
| −8 | 4 | −2 | −2 | −2 | 0 | 0 |
For positive numbers the two methods agree. For negative numbers that do not divide evenly they differ by n. A negative remainder is corrected by adding n: −1 + 3 = 2.
Calculating with remainders
Sums and products can be worked remainder by remainder:
- (a + b) mod n = ((a mod n) + (b mod n)) mod n
- (a · b) mod n = ((a mod n) · (b mod n)) mod n
Days of the week. If today is a Thursday, which day is it in 100 days? 100 mod 7 = 2, so it is two days after Thursday: Saturday.
A large power. 2¹⁰ mod 7: since 2³ = 8 ≡ 1 (mod 7), 2⁹ = (2³)³ ≡ 1, and 2¹⁰ = 2⁹ · 2 ≡ 2 (mod 7). Check: 1,024 = 7 · 146 + 2.
Common mistakes
- A negative remainder. a mod n lies between 0 and n − 1. If a calculator gives −1 for −7 mod 3, add 3.
- Cutting off instead of rounding down. ⌊−2.33⌋ is −3. Dropping the decimals gives −2, which is only right for positive numbers.
- Confusing congruence with equality. 38 ≡ 14 (mod 12) does not mean 38 = 14, only that they leave the same remainder.
- A modulus of 0. a mod 0 has no value, because nothing can be divided by 0. The definition is for n ≥ 1.
Common questions
What does mod stand for?
Mod is short for the Latin modulus, roughly “measure”. a mod n is the remainder on division by n, and n is called the
modulus.
How can I tell that a number is divisible by n?
A number a is divisible by n exactly when a mod n = 0, that is a ≡ 0 (mod n). The
greatest common divisor rests on the same idea.
Why do spreadsheets and programming languages disagree?
The MOD function in Excel and Google Sheets returns a remainder with the sign of the divisor, so MOD(−7, 3) is 2. The %
operator in several programming languages, JavaScript and C among them, returns −1 instead, because it cuts the
quotient off.