Number TheoryFoundationsApplications

Modular Arithmetic

Modular arithmetic is the mathematics of remainders — counting that wraps around after reaching a fixed modulus. It underlies nearly every cryptographic protocol, hash function, and digital clock you have ever used.

Clocks, calendars, and cryptographic keys all run on the same arithmetic. The idea is simple: when you reach the top, you wrap around.


Why this matters

Modular arithmetic is the foundation of number theory and modern cryptography. RSA encryption, elliptic curve cryptography, and the SHA family of hash functions are all built on modular arithmetic. It is also how computers handle integer overflow, how ISBN check digits work, and how the days of the week cycle. Understanding it deeply opens the door to a substantial part of applied and pure mathematics.

The basic idea

Fix a positive integer nn called the modulus. Two integers aa and bb are congruent modulo nn, written

ab(modn)a \equiv b \pmod{n}

if their difference aba - b is divisible by nn. Equivalently, aa and bb have the same remainder when divided by nn.

Examples:

  • 172(mod5)17 \equiv 2 \pmod{5}, because 172=15=3×517 - 2 = 15 = 3 \times 5.
  • 34(mod7)-3 \equiv 4 \pmod{7}, because 34=7=(1)×7-3 - 4 = -7 = (-1) \times 7.
  • 1000(mod10)100 \equiv 0 \pmod{10}, because 100100 is divisible by 1010.

The notation amodna \bmod n (without the \equiv) denotes the remainder: a number in {0,1,,n1}\{0, 1, \ldots, n-1\}. So 17mod5=217 \bmod 5 = 2.

Clock arithmetic

The familiar 12-hour clock is modular arithmetic with n=12n = 12. At 11 o’clock, one hour later is not the 12th hour on an infinite number line — it wraps to 12, and after another hour, to 1. The arithmetic rule is:

(current hour+hours elapsed)mod12(\text{current hour} + \text{hours elapsed}) \bmod 12

(with the convention that 00 is displayed as 1212).

How computation works

Congruence behaves well under addition, subtraction, and multiplication:

If aa(modn) and bb(modn), then:\text{If } a \equiv a' \pmod{n} \text{ and } b \equiv b' \pmod{n}, \text{ then:}

a+ba+b(modn)a + b \equiv a' + b' \pmod{n} abab(modn)a - b \equiv a' - b' \pmod{n} abab(modn)a \cdot b \equiv a' \cdot b' \pmod{n}

This means you can reduce intermediate results at each step, which is critical for efficient computation with very large numbers.

Worked example — computing 7⁵ mod 13

Computing 75(mod13)7^5 \pmod{13} directly: 75=168077^5 = 16807. Then 16807mod13=?16807 \bmod 13 = ?

Better approach — reduce at each step:

717(mod13)7^1 \equiv 7 \pmod{13} 7249493×13493910(mod13)7^2 \equiv 49 \equiv 49 - 3 \times 13 \equiv 49 - 39 \equiv 10 \pmod{13} 737×10=70705×13=70655(mod13)7^3 \equiv 7 \times 10 = 70 \equiv 70 - 5 \times 13 = 70 - 65 \equiv 5 \pmod{13} 747×5=35352×13=35269(mod13)7^4 \equiv 7 \times 5 = 35 \equiv 35 - 2 \times 13 = 35 - 26 \equiv 9 \pmod{13} 757×9=63634×13=635211(mod13)7^5 \equiv 7 \times 9 = 63 \equiv 63 - 4 \times 13 = 63 - 52 \equiv 11 \pmod{13}

So 7511(mod13)7^5 \equiv 11 \pmod{13}. This reduction-at-each-step technique, called modular exponentiation, is the core operation in RSA encryption — applied to numbers with hundreds of digits.

Modular arithmetic and groups

The set Z/nZ={0,1,2,,n1}\mathbb{Z}/n\mathbb{Z} = \{0, 1, 2, \ldots, n-1\} under addition modulo nn is a group. This connects modular arithmetic directly to abstract algebra.

For multiplication, the story is more selective: the set of integers relatively prime to nn, written (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times, forms a group under multiplication modulo nn. Its order (size) is given by Euler’s totient function ϕ(n)\phi(n).

For example, (Z/7Z)×={1,2,3,4,5,6}(\mathbb{Z}/7\mathbb{Z})^\times = \{1,2,3,4,5,6\} has order ϕ(7)=6\phi(7) = 6, and by Fermat’s little theorem, a61(mod7)a^6 \equiv 1 \pmod{7} for any aa not divisible by 77.

Inverses modulo nn

When does aa have a multiplicative inverse mod nn? Exactly when gcd(a,n)=1\gcd(a, n) = 1. In that case, there exists bb such that ab1(modn)ab \equiv 1 \pmod{n}.

Example: Find the inverse of 33 modulo 77.
We need 3b1(mod7)3b \equiv 1 \pmod 7. Testing: 3×5=15=2×7+11(mod7)3 \times 5 = 15 = 2 \times 7 + 1 \equiv 1 \pmod 7. So 315(mod7)3^{-1} \equiv 5 \pmod 7.
In general, this is computed efficiently using the extended Euclidean algorithm.

Common misconception

The remainder operation and modular congruence behave unexpectedly for negative numbers in most programming languages. In Python, 3mod5=2-3 \bmod 5 = 2 (mathematically correct). In C or Java, 3%5=3-3 \% 5 = -3 (truncation toward zero). The mathematical definition requires the remainder to lie in {0,1,,n1}\{0, 1, \ldots, n-1\}. When working with negative numbers, always check your language’s convention — or reduce manually by adding nn until the result is non-negative.


Self-check
What is 53 mod 7?
Which of the following is true?
The multiplicative inverse of 4 modulo 9 is:

Stay in the loop

New concept pages ship regularly. Get a quiet note when something worth reading goes live.

No spam. Unsubscribe any time.