TExcelTAD
Home › Discrete Mathematics › Modular Arithmetic

Modular Arithmetic

Modular arithmetic is arithmetic with remainders: clock time, days of the week, angles, and every fixed-width integer in a CPU. Gauss introduced the notation a≡b(modn)a \equiv b \pmod{n}, and with it you can add, subtract and multiply remainders as if they were ordinary numbers. For a school-level introduction see Modular Arithmetic in Basic Math.

Congruence

For an integer n>1n > 1, aa is congruent to bb modulo nn,

a≡b(modn)  ⟺  n∣(a−b)a \equiv b \pmod{n} \iff n \mid (a - b)

Equivalently, aa and bb leave the same remainder when divided by nn: rem⁡(a,n)=rem⁡(b,n)\operatorname{rem}(a, n) = \operatorname{rem}(b, n). Examples: 29≡5(mod12)29 \equiv 5 \pmod{12} (5 o'clock), −1≡6(mod7)-1 \equiv 6 \pmod 7, 10≡1(mod9)10 \equiv 1 \pmod 9.

Congruence modulo nn is an equivalence relation: reflexive, symmetric and transitive. It splits Z\mathbb{Z} into nn residue classes, represented by 0,1,…,n−10, 1, \ldots, n - 1.

Arithmetic is compatible

Theorem. If a≡a′a \equiv a' and b≡b′(modn)b \equiv b' \pmod{n}, then

a+b≡a′+b′,a−b≡a′−b′,ab≡a′b′(modn)a + b \equiv a' + b', \qquad a - b \equiv a' - b', \qquad ab \equiv a'b' \pmod{n}

Proof for products. ab−a′b′=a(b−b′)+b′(a−a′)ab - a'b' = a(b - b') + b'(a - a'), and nn divides both terms. ■\blacksquare

So you can reduce at any time. This is what makes huge computations feasible: to find the last two digits of 720267^{2026}, work modulo 100 and never handle a number bigger than 99299^2.

python · try it yourself
def powmod(a, e, n):
    """Repeated squaring, reducing modulo n after every multiplication."""
    result, a = 1, a % n
    while e:
        if e & 1:
            result = result * a % n
        a = a * a % n
        e >>= 1
    return result

print("7^2026 mod 100 =", powmod(7, 2026, 100), "  built-in:", pow(7, 2026, 100))
print("last digits of 7^2026 by full computation:", str(7 ** 2026)[-2:])
print("digits in 7^2026:", len(str(7 ** 2026)))
7^2026 mod 100 = 49   built-in: 49
last digits of 7^2026 by full computation: 49
digits in 7^2026: 1713

The fast version needs about log⁡22026≈11\log_2 2026 \approx 11 squarings; the full number has 1713 digits. RSA computes me mod nm^e \bmod n for 2048-bit nn and ee in exactly this way.

Division is the exception

Cancellation fails in general: 2⋅3≡2⋅8(mod10)2 \cdot 3 \equiv 2 \cdot 8 \pmod{10} (both are 6 or 16, remainder 6), but 3≢8(mod10)3 \not\equiv 8 \pmod{10}. You may cancel a factor kk only when gcd⁡(k,n)=1\gcd(k, n) = 1. The multiplication tables show why: modulo 10, multiplying by 2 is not a bijection, but multiplying by 3 is.

python · try it yourself
n = 10
for k in [2, 3, 5, 7]:
    row = [k * x % n for x in range(n)]
    print(f"{k} * x mod {n}: {row}  bijection: {sorted(row) == list(range(n))}")
2 * x mod 10: [0, 2, 4, 6, 8, 0, 2, 4, 6, 8]  bijection: False
3 * x mod 10: [0, 3, 6, 9, 2, 5, 8, 1, 4, 7]  bijection: True
5 * x mod 10: [0, 5, 0, 5, 0, 5, 0, 5, 0, 5]  bijection: False
7 * x mod 10: [0, 7, 4, 1, 8, 5, 2, 9, 6, 3]  bijection: True

Rows for 3 and 7 are permutations of 0,…,90, \ldots, 9, so each has a 1 somewhere: 3⋅7≡1(mod10)3 \cdot 7 \equiv 1 \pmod{10}. Rows for 2 and 5 never reach 1. When n=pn = p is prime, every nonzero kk is coprime to pp, so every nonzero number has an inverse: Zp\mathbb{Z}_p is a field. (See also Matrices mod p.)

Zn\mathbb{Z}_n

The set Zn={0,1,…,n−1}\mathbb{Z}_n = \{0, 1, \ldots, n - 1\} with ++ and ⋅\cdot taken modulo nn satisfies the familiar rules: associativity, commutativity, distributivity, identities 0 and 1. That is why a 32-bit CPU computes correctly in Z232\mathbb{Z}_{2^{32}}: unsigned overflow is not an error, it is arithmetic modulo 2322^{32}.

In software

  • Hashing: h = (h * 31 + ord(c)) % M processes a string with all intermediate values bounded.
  • Circular buffers and round-robin: (i + 1) % n.
  • Calendars: the weekday of a date is computed modulo 7 (Zeller's congruence).
  • Checksums: ISBN-10 uses mod 11, IBAN uses mod 97.
  • Integer overflow in C, Java and Rust's wrapping types is arithmetic in Z232\mathbb{Z}_{2^{32}} or Z264\mathbb{Z}_{2^{64}}.

Exercises

Exercise 1: Congruent or not?

(a) 47≡2(mod9)47 \equiv 2 \pmod 9? (b) −8≡13(mod7)-8 \equiv 13 \pmod 7? (c) 100≡1(mod11)100 \equiv 1 \pmod{11}?

Answer

(a) yes; (b) yes; (c) yes.

Solution

(a) 47−2=45=5⋅947 - 2 = 45 = 5 \cdot 9. (b) −8−13=−21=−3⋅7-8 - 13 = -21 = -3 \cdot 7. (c) 100−1=99=9⋅11100 - 1 = 99 = 9 \cdot 11.

Exercise 2: Last digit by hand

Find the last digit of 31003^{100}.

Answer
Solution

34=81≡1(mod10)3^4 = 81 \equiv 1 \pmod{10}, so

3100=(34)25≡125=1(mod10)3^{100} = (3^4)^{25} \equiv 1^{25} = 1 \pmod{10}

Exercise 3: Day of the week

Today is a Wednesday. What day is it 10610^{6} days from now?

Answer

Thursday.

Solution

10≡3(mod7)10 \equiv 3 \pmod 7, so 106≡36=729≡1(mod7)10^6 \equiv 3^6 = 729 \equiv 1 \pmod 7 (729=104⋅7+1729 = 104 \cdot 7 + 1). So 10610^6 days is a whole number of weeks plus one day: one day after Wednesday is Thursday.

python · try it yourself
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
print(10 ** 6 % 7, days[(days.index("Wed") + 10 ** 6) % 7])
1 Thu

Exercise 4: 32-bit overflow

A 32-bit unsigned counter holds 42949672904294967290 and is increased by 10. What value does it hold?

Answer
Solution

4294967290+10=4294967300≡4294967300−232=4(mod232)4294967290 + 10 = 4294967300 \equiv 4294967300 - 2^{32} = 4 \pmod{2^{32}}.

Test yourself

a≡b(modn)a \equiv b \pmod{n} means…

  1. a=ba = b
  2. n∣a−bn \mid a - b
  3. a∣ba \mid b
  4. a⋅b=na \cdot b = n

Which number has a multiplicative inverse modulo 12?

  1. 4
  2. 6
  3. 8
  4. 5

Why can RSA compute me mod nm^e \bmod n quickly for enormous ee?

  1. By computing mem^e exactly first
  2. By repeated squaring with reduction mod nn after each step
  3. Because ee is always small
  4. It cannot