Modular Arithmetic
You already do modular arithmetic every day — you just call it telling the time. A clock
doesn't count up forever; once it reaches 12 it resets and starts again
from 1. Think of a 24-hour clock: 14{:}00 is
the same time of day as 2{:}00, because
14 = 12 + 2 — the hours have wrapped around once. We write this as
14 \equiv 2 \pmod{12},
read "14 is congruent to 2, modulo
12." Modular arithmetic takes this everyday "wrapping"
idea and turns it into a precise, powerful tool — arguably the single most important one in number
theory, and the quiet engine behind clocks, calendars, check digits, and internet cryptography
alike.
Clocks are only the beginning — anything that cycles back to its starting point is secretly doing
modular arithmetic. A car's trip odometer rolls over from 999.9 back to
0.0; a compass bearing of 370^\circ is really
just 10^\circ, since angles wrap modulo 360^\circ;
a combination-lock dial marked 0 to 39 wraps
modulo 40 every time you spin it past the top. Even music does it: there
are only 12 notes before the pattern of pitches repeats an octave higher,
so a piano keyboard is, secretly, another clock face — arithmetic modulo
12 again. Whenever "one more" can loop back to the start, some modulus
is quietly at work.
Congruence
Fix a positive integer n, the modulus. Two integers
are congruent modulo n if they leave the same
remainder
on division by n — equivalently, if their difference is a multiple of
n:
a \equiv b \pmod{n} \iff n \mid (a - b).
On a 12-hour clock 15 \equiv 3 \pmod{12},
because 15 - 3 = 12, a multiple of 12. Step
the dial below to see numbers wrap.
A few more clock sums, all reduced back into the range 1 to 12:
| Hours after | Ordinary sum | Clock time (mod 12) |
| 9 o'clock, +5 hours | 14 | 2 |
| 10 o'clock, +6 hours | 16 | 4 |
| 11 o'clock, +4 hours | 15 | 3 |
Worked example: adding and multiplying, mod 7
Pick a small modulus, say n = 7, and try
5 + 4. Ordinarily that's 9, but modulo
7 we keep subtracting 7 until we land in the
range 0 to 6:
5 + 4 = 9 = 7 + 2 \implies 5 + 4 \equiv 2 \pmod{7}.
Multiplication wraps the same way. Take 3 \times 5 = 15. Since
15 = 2 \times 7 + 1,
3 \times 5 \equiv 1 \pmod{7}.
A handful more, all reduced to a remainder between 0 and
6:
| Expression | Ordinary value | Result mod 7 |
| 6 + 6 | 12 | 5 |
| 4 + 5 | 9 | 2 |
| 4 \times 4 | 16 | 2 |
| 6 \times 6 | 36 | 1 |
No matter how large the ordinary sum or product gets, the mod-7 answer
is always one of just seven values: 0, 1, 2, 3, 4, 5, 6. That's the whole
trick — and the whole power — of working modulo n.
Worked example: what day is 100 days from now?
Suppose today is Wednesday, and you want to know what day of the week it will be
100 days from now. You could count forward one day at a time — or you
could notice that the days of the week repeat every 7 days, so only the
remainder of 100 after dividing by 7
actually matters.
100 = 14 \times 7 + 2 \implies 100 \equiv 2 \pmod{7}.
So 100 days from now lands on the same day of the week as just
2 days from now. Two days after Wednesday is
Friday — and that's the answer, without ever counting past
7. The same shortcut works for a thousand days, a million days, or any
number at all: reduce it modulo 7 first, then count on from there.
The trick works just as well running backwards. What day was it
50 days ago, if today is Wednesday again? Since
50 = 7 \times 7 + 1, going back 50 days is
the same as going back just 1 day — landing on
Tuesday. Notice we never had to think about a "negative" number of days at all;
we simply reduced 50 modulo 7 first, exactly
as before.
It's an equivalence relation
Congruence behaves just like equality: it is reflexive
(a \equiv a), symmetric, and transitive. So all the
integers leaving a given remainder collapse into one residue class. Modulo
5 there are exactly five of them:
\{\dots,-5,0,5,10,\dots\},\ \{\dots,-4,1,6,11,\dots\},\ \dots,\ \{\dots,-1,4,9,\dots\}.
Each class is named by its remainder, 0, 1, 2, 3, 4. Working modulo
n means working with these n classes instead
of the infinitely many integers — a colossal simplification.
This is exactly what a clock face already shows you. The twelve numbers printed round the dial
aren't twelve special integers — each one is the label for an entire infinite class:
the "3" on the dial stands for every integer congruent to
3 modulo 12, meaning
\dots, -21, -9, 3, 15, 27, 39, \dots all point at the very same spot.
A clock face is a residue-class diagram you've been reading since you were a small child, long
before anyone called it that.
The key insight: arithmetic with remainders alone
Congruence isn't just a notation; it respects arithmetic. If
a \equiv a' and b \equiv b' modulo
n, then a + b \equiv a' + b' and
a \times b \equiv a' \times b' too — sums and products of congruent
numbers stay congruent. That means you never have to work with the huge original numbers at all;
their remainders carry all the information arithmetic needs.
Check it: take 23 + 15 = 38 modulo 7. Reducing
first, 23 \equiv 2 and 15 \equiv 1 \pmod 7, so
their remainders add to 2 + 1 = 3. Reducing the full sum directly,
38 = 5 \times 7 + 3 \equiv 3 \pmod 7 — the very same answer, either way
round. That single fact — that + and \times
"pass through" congruence — is what underlies divisibility tricks, check digits, hashing, and all
of cryptography. Next we turn the n residue classes into a genuine
number system of their own, with
arithmetic mod n.
The congruence symbol \equiv looks like a triple-barred equals sign, and
it behaves like equality for the purposes of adding and multiplying — but it does not mean
the two numbers are the same number. 14 \equiv 2 \pmod{12} is true, yet
14 \ne 2: they're different integers that happen to land on the same
mark of a 12-hour clock. Always keep the modulus written — "14 \equiv 2"
on its own, with no \pmod{12}, is simply false.
Negative numbers trip people up too. What is -1 \pmod{12}? It's tempting
to say the question doesn't make sense, but it wraps around correctly, just like a clock running
backwards one hour from 12 lands on 11:
-1 \equiv 11 \pmod{12}, \qquad \text{since } -1 - 11 = -12 \text{ is a multiple of } 12.
In general, keep adding the modulus to a negative number until the result lands in
0, 1, \dots, n-1. Get this wrong and every calendar or clock calculation
that runs "backwards" comes out off by one full cycle.
Every time a shop scanner beeps at a barcode, or a website accepts your credit card number, modular
arithmetic is quietly checking your work. Barcodes (UPC codes), credit card numbers (via the
Luhn algorithm), and ISBN book numbers all end in a
check digit — one extra digit computed from all the others using a fixed recipe of
multiplying, adding, and reducing modulo some small number, usually 9,
10, or 11.
When you type the number back in, the machine redoes the same calculation and checks that the
check digit still matches. Mistype a single digit, or accidentally swap two neighbouring digits —
by far the two most common typing errors — and the modular check almost always fails, so the
machine rejects the number before it ever reaches a database. No expensive error-correcting
hardware required: just a few multiplications and a division with remainder, courtesy of
the division algorithm.