Mathematics / Modular arithmetic

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.

Beginner7 steps~7 min
The ring — the numbers 0 to n − 1, wrapping round
Start — where the count begins
Remainder — where the result lands on the ring
Full group — a multiple of n, which changes nothing
// THE SHORT VERSION

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

STEPS01 / 07

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 01 / 07 Clock arithmetic
THE MATH
9 + 5 = 14 14 − 12 = 2 9 + 5 ≡ 2 (mod 12)
  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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  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.
  7. 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.
VALUES SO FAR
9 + 52 (mod 12)clock
17 mod 5····remainder
17 ≡ 2····congruent
5 + 4····add
5 × 4····multiply
3 × 5····inverse
62 mod 7····bucket
65^17····RSA
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?