CipherMatrix

Number Theory · Beginner · 8 min read

Modular Arithmetic for Beginners

Modular arithmetic is the arithmetic of remainders, and it is the language of number theory and cryptography. If you can read a clock, you already understand the central idea.

Congruence

We say a ≡ b (mod m) when m divides a − b, which means a and b leave the same remainder after division by m. On a 12-hour clock, 15 o'clock is the same as 3 o'clock because 15 ≡ 3 (mod 12). The remainder of a divided by m, written a mod m, is the representative between 0 and m − 1.

Arithmetic rules

Congruences behave like equations for addition, subtraction and multiplication: if a ≡ b and c ≡ d (mod m), then a + c ≡ b + d and ac ≡ bd. This lets you reduce numbers at any moment. To find the last digit of 7100, for instance, you only need to work modulo 10.

Division and inverses

Division is the delicate operation. The number a has an inverse modulo m only when gcd(a, m) = 1; the inverse is the x with ax ≡ 1. Dividing by a then means multiplying by x. To solve ax ≡ b (mod m), multiply both sides by the inverse of a. If gcd(a, m) = g is greater than 1, solutions exist only when g divides b, and then there are g of them.

Fermat's little theorem

If p is prime and p does not divide a, then ap−1 ≡ 1 (mod p). It gives a quick way to reduce large exponents and to find inverses modulo a prime: a−1 ≡ ap−2.

Key formulas

Solved example

Solve 5x ≡ 3 (mod 13)

  1. gcd(5, 13) = 1, so 5 has an inverse modulo 13.
  2. Find it: 5 · 8 = 40 = 3 · 13 + 1, so 5−1 ≡ 8.
  3. Multiply both sides by 8: x ≡ 8 · 3 = 24 ≡ 11 (mod 13).
  4. Check: 5 · 11 = 55 = 4 · 13 + 3, so 5 · 11 ≡ 3 (mod 13).

Practice questions

Try each question first, then open the answer.

1. What is 123 mod 7?

4, since 7 · 17 = 119 and 123 − 119 = 4.

2. Solve 3x ≡ 5 (mod 11).

The inverse of 3 is 4 (3 · 4 = 12 ≡ 1), so x ≡ 20 ≡ 9. Check: 3 · 9 = 27 = 2 · 11 + 5.

3. What is 210 mod 11?

1. By Fermat, 210 ≡ 1 (mod 11); directly, 1024 = 93 · 11 + 1.

Calculators for this topic