Modular Arithmetic
Arithmetic mod n only cares about remainders, the way a clock wraps around after 12. Two numbers that leave the same remainder count as the same, and you can add, subtract and multiply them like ordinary numbers, reducing as often as you like to keep things small.
What the symbols mean
- a is congruent to b
a and b leave the same remainder on division by n.
- Modulus
The number you wrap around at.
Explanation
Modular arithmetic is simply arithmetic where you only keep the remainder, so numbers wrap around like the hours on a clock. "Mod" is short for modulo, Latin for "by the measure of": you measure a number by and keep what's left over. You already use it to tell time and to work out days of the week, and every modern cipher runs on it. The statement looks formal, but it's just " and land on the same spot on an -hour clock," and (n goes evenly into the gap between them) is the same idea said with division. Nothing more to it than that.
: 17 o'clock is 5 o'clock. Every integer is congruent to exactly one of mod , its remainder from the division you saw two lessons ago.
Rules. If and , then and . Why should that be true? Write and . Then , and everything past is a multiple of , so it vanishes mod . Addition works the same way. That's what makes this so handy: you can reduce at every step and keep the numbers small. Division is the exception; it needs inverses, which are the next lesson.
Big powers. To find , notice , so . Once some power is 1, the powers repeat in a cycle, and you only need the exponent's remainder. In general, repeated squaring computes with about multiplications.
Divisibility tests are modular facts in disguise. Since , every power of 10 is also , so a number is congruent to its digit sum mod 9, and it's divisible by 9 exactly when its digit sum is.
Calendars. Days of the week are arithmetic mod 7. 100 days from Monday: , so it's a Wednesday.
See it
Worked examples
Find .
- 1
Reduce the base first: , so .
Practice
Type your answer and check it. Solve them all to complete the lesson.
What is ?
Today is Monday. What day of the week is it 100 days from now?
Find .
Is 123456789 divisible by 9?
An ISBN-10 ends with a check digit chosen so that . For the ISBN 0-306-40615-?, the first nine digits give . Find .
What is the last digit of ?
The hardest one here. Get it, and you've got this lesson.
Find the remainder when is divided by 13.
Where it's used
Clocks, calendars, hash tables, check digits on barcodes and every modern cipher run on modular arithmetic.