What is modular arithmetic? Clock math and where it's used in real life
If it's 10 o'clock and you add 5 hours, you get 3 o'clock, not 15. You've just done modular arithmetic, sometimes called "clock arithmetic".
The basic idea
Working modulo n means you only care about the remainder after dividing by n. Numbers "wrap around" when they reach n.
- 15 mod 12 = 3
- 17 mod 5 = 2
- 20 mod 4 = 0
We say two numbers are congruent mod n if they leave the same remainder. For example, 3 and 15 are congruent mod 12.
Arithmetic still works
You can add, subtract and multiply, then reduce. For instance, to find the last digit of a huge product, you only need the last digits of the factors, because that's arithmetic mod 10.
Division is trickier: it only works neatly when the number you're dividing by shares no common factor with n. When n is prime, every non-zero number has an inverse, which makes prime moduli especially useful.
Where you meet it
- Time and calendars – days of the week cycle mod 7. Working out what weekday a date falls on is modular arithmetic.
- Check digits – ISBNs, bank account formats and many ID numbers include a final digit computed with modular arithmetic to catch typos.
- Cryptography – RSA and many other systems rely on modular exponentiation with very large numbers. It's easy to compute in one direction and very hard to reverse without a secret.
- Computers – fixed-size integers naturally wrap around, and hash tables use mod to assign items to buckets.
- Music – the 12 notes of the chromatic scale repeat every octave, a mod 12 pattern.
Discussion: Where else have you spotted modular arithmetic? And is "clock math" a helpful way to teach it, or does it oversimplify?
Comments · 4
It's the math behind checksums, credit card validation and a lot of encryption. A tiny idea with a huge footprint.
If you've ever used the % operator in code, that's it. Handy for wrapping an index around the end of an array.
Why is it useful for cryptography specifically? Is it that doing it backwards is hard?
Roughly, yes. Multiplying is easy under a modulus but undoing it is extremely hard, and that gap powers many encryption systems.