Modular arithmetic
Arithmetic on a ring, where counting wraps round like a clock: remainders, congruence, adding and multiplying mod n, the inverse that stands in for division, and where it is used.
In 30 seconds
Modular arithmetic counts round a ring instead of along a line. With modulus 12, as on a clock, 9 + 5 gives 2: once you pass 12 you start again.
A number’s place on the ring is its remainder after dividing by the modulus. Numbers with the same remainder are congruent and can replace one another when adding or multiplying.
Division is replaced by the inverse, the number that multiplies back to 1. Hash tables use remainders to pick a slot, and public-key cryptography is built from powers and inverses on such rings.
Modular arithmetic, step by step
Real hash tables scramble a key with a hash function before taking the remainder. For negative numbers, the % operator of some programming languages returns a negative result rather than 0 to n − 1.
- Step 1: Clock arithmetic. Five hours after 9 o’clock it is 2 o’clock, not 14. A clock face counts round a ring and starts again after 12. Modular arithmetic is that idea for any ring size, called the modulus. Positions are numbered from 0, so 12 itself is written 0.
- Step 2: The remainder. Where a number lands on the ring is its remainder: divide by the modulus and keep what is left over. Seventeen holds three full groups of 5 with 2 to spare, so 17 mod 5 is 2. The remainder is always smaller than the modulus.
- Step 3: Congruent numbers. Sort the whole numbers by their remainder mod 5 and they fall into five columns. Numbers in one column, such as 2, 7, 12 and 17, are called congruent mod 5: they sit at the same place on the ring and differ only by full turns.
- Step 4: Adding wraps around. To add mod 7, add as usual and take the remainder: 5 + 4 is 9, which is 2. Congruent numbers can stand in for each other. 19 and 11 land on 5 and 4, so their sum must land where 5 + 4 does, and 30 mod 7 is indeed 2.
- Step 5: Multiplying mod n. Multiplying is repeated adding: four jumps of 5 from 0 pass 5, 10, 15 and 20, which sit on the ring at 5, 3, 1 and 6. So 5 × 4 is 6 mod 7. Again any number may be swapped for its remainder first: 19 × 11 = 209 also leaves 6.
- Step 6: The modular inverse. There is no dividing on the ring, but there is undoing. The inverse of 3 mod 7 is the number that multiplies with 3 to give 1: trying each in turn finds 5. Multiplying by 5 then plays the part of dividing by 3. Mod 6 the search fails, because 3 and 6 share a factor.
- Step 7: Hashing and cryptography. A hash table with 7 buckets files a key under its remainder mod 7, so any key, however large, gets a valid slot. Public-key cryptography raises numbers to large powers mod n: results stay small, and inverses supply the key that undoes the lock.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- Fermat’s little theoremOn a ring of prime size p, the power p − 1 of any number lands on 1, and RSA uses it. Intermediate
- Diffie–Hellman key exchangeTwo strangers agree on a shared secret over a public line. Beginner
- RSA encryptionPublic keys, private keys, and why factoring big numbers is hard. Beginner
Spotted a mistake on this page?