Modular Arithmetic

⚡ Key Takeaways

Modular arithmetic is arithmetic with remainders: numbers "wrap around" after reaching a fixed value called the modulus. Two integers are equivalent ("congruent") if they leave the same remainder when divided by the modulus nn, written a≡b(modn)a \equiv b \pmod{n}. This system powers cryptography, computer science, calendar calculations, and checksum systems. Formalized by Carl Friedrich Gauss in his 1801 Disquisitiones Arithmeticae.

💡 The Core Insight

Forget the value of a number and remember only its remainder after division by nn. Because remainders behave predictably under ++, −-, and ×\times, an entire clock-like world of arithmetic emerges in which there are only nn distinct numbers. The clock is the perfect mental model: 10+5=1510 + 5 = 15, but on a 12-hour clock 10+5≡310 + 5 \equiv 3.

Formal Statement

DefinitionCongruence Modulo n

Let nn be a positive integer (the modulus). For integers aa and bb, we say aa is congruent to bb modulo nn, written

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

which means there exists an integer kk such that a−b=kna - b = kn. Here:

  • a,ba, b are integers,
  • n≥1n \ge 1 is the modulus,
  • n∣(a−b)n \mid (a-b) means "nn divides a−ba-b exactly,"
  • kk is some integer (positive, negative, or zero).

Derivation hint: "Same remainder" is equivalent to "difference divisible by nn" — subtract the two division statements a=q1n+ra = q_1 n + r and b=q2n+rb = q_2 n + r to get a−b=(q1−q2)na-b=(q_1-q_2)n.

DefinitionResidue Class and Set of Residues

The residue class of aa modulo nn is the set of all integers congruent to aa:

2
[a]n={ a+kn:k∈Z }.\quad [a]_n = \{\, a + kn : k \in \mathbb{Z} \,\}.

There are exactly nn distinct residue classes, and the standard set of representatives is

3
Z/nZ={ [0],[1],[2],…,[n−1] }.\quad \mathbb{Z}/n\mathbb{Z} = \{\, [0], [1], [2], \dots, [n-1] \,\}.

Derivation hint: The Division Algorithm guarantees every integer a=qn+ra = qn + r with a unique remainder 0≤r<n0 \le r < n, so every integer lands in exactly one of the nn classes labeled by rr.

TheoremCongruence is an Equivalence Relation

For any fixed modulus nn, congruence modulo nn satisfies: 1. Reflexive: a≡a(modn)a \equiv a \pmod n, 2. Symmetric: if a≡ba \equiv b then b≡a(modn)b \equiv a \pmod n, 3. Transitive: if a≡ba \equiv b and b≡cb \equiv c then a≡c(modn)a \equiv c \pmod n. Therefore it partitions Z\mathbb{Z} into disjoint residue classes.

TheoremCompatibility with Arithmetic

If a≡b(modn)a \equiv b \pmod n and c≡d(modn)c \equiv d \pmod n, then:

4
a+c≡b+d(modn),\quad a + c \equiv b + d \pmod n,
5
a−c≡b−d(modn),\quad a - c \equiv b - d \pmod n,
6
ac≡bd(modn),\quad a c \equiv b d \pmod n,
7
am≡bm(modn)for any integer m≥1.\quad a^m \equiv b^m \pmod n \quad \text{for any integer } m \ge 1.

Derivation hint: Write a−b=kna-b = kn and c−d=ℓnc-d=\ell n; then (a+c)−(b+d)=(k+ℓ)n(a+c)-(b+d)=(k+\ell)n and ac−bd=ac−bc+bc−bd=c(a−b)+b(c−d)=(ck+bℓ)nac-bd = ac-bc+bc-bd = c(a-b)+b(c-d) = (ck+b\ell)n. Equation [7] follows by repeated application of [6].

Why It Holds / Derivation

:::proof[Multiplication Respects Congruence — Equation [6]] We prove that if a≡b(modn)a \equiv b \pmod n and c≡d(modn)c \equiv d \pmod n, then ac≡bd(modn)ac \equiv bd \pmod n.

Step 1. By the definition of congruence, a−b=kna - b = kn and c−d=ℓnc - d = \ell n for some integers k,ℓk, \ell.

Step 2. Consider the difference ac−bdac - bd. Add and subtract bcbc:

ac−bd=ac−bc+bc−bd.ac - bd = ac - bc + bc - bd.

Step 3. Factor each pair:

ac−bc+bc−bd=c(a−b)+b(c−d).ac - bc + bc - bd = c(a - b) + b(c - d).

Step 4. Substitute the expressions from Step 1:

c(a−b)+b(c−d)=c(kn)+b(ℓn)=(ck+bℓ) n.c(a-b) + b(c-d) = c(kn) + b(\ell n) = (ck + b\ell)\,n.

Step 5. Since ck+bℓck + b\ell is an integer, n∣(ac−bd)n \mid (ac - bd), which by definition means ac≡bd(modn)ac \equiv bd \pmod n. ■\blacksquare

⚠️ Division Does NOT Always Work

Notice equations [4]–[7] cover +,−,×+,-,\times and powers — but not division. You cannot simply "cancel" a common factor. For example 6≡0(mod6)6 \equiv 0 \pmod 6 but you cannot conclude 3≡03 \equiv 0 by "dividing both sides by 2." Cancellation is valid only when the factor is coprime to the modulus (see the Modular Multiplicative Inverse below).

mathematician · Carl Friedrich Gauss | 1777–1855

Often called the "Prince of Mathematicians," Gauss invented the modern notation and theory of congruences while still in his early twenties. In his 1801 masterpiece Disquisitiones Arithmeticae, written when he was 24, he introduced the symbol "≡\equiv" and systematically developed congruences as a rigorous tool, proving the Chinese Remainder Theorem in general form and laying the groundwork for quadratic reciprocity. His insight was that treating remainders as first-class objects unified scattered observations of earlier number theorists (Fermat, Euler, Lagrange) into a single elegant algebraic structure that underlies essentially all of Number Theory today.

Building Intuition

🧠 The Clock and the Number Line Wrapped into a Circle

Imagine the integer number line stretched out, then wrapped around a circle of circumference nn. Every point that lands at the same spot on the circle is congruent. A 12-hour clock is modulus 12: 3 o'clock, 15 o'clock, and 27 o'clock all point at the same place because 3≡15≡27(mod12)3 \equiv 15 \equiv 27 \pmod{12}. Adding hours is just rotating around the circle. When you "go off the top" you reappear at the bottom — that's the wrap-around. Because the circle has only nn positions, arithmetic stays bounded and finite, no matter how large the original numbers were. This is why modular arithmetic lets a computer multiply enormous numbers and keep only the small remainder.

Figure
A clock modeling arithmetic modulo 12: the integers wrap around a circle of 12 positions
0 1 2 3 4 5 6 7 8 9 10 11 10 + 5 = 15 ≡ 3 (mod 12)

Worked Examples

ExampleComputing a Large Power Modulo n

Given: Find 7100  137^{100} \bmod 13.

Step 1 — Find a small cycle. Compute powers of 7 modulo 13: 71≡77^1 \equiv 7, 72=49≡49−39=107^2 = 49 \equiv 49 - 39 = 10, 73≡7⋅10=70≡70−65=57^3 \equiv 7 \cdot 10 = 70 \equiv 70 - 65 = 5, 74≡7⋅5=35≡35−26=97^4 \equiv 7 \cdot 5 = 35 \equiv 35 - 26 = 9.

Step 2 — Use Fermat's Little Theorem. Since 13 is prime and gcd⁡(7,13)=1\gcd(7,13)=1:

712≡1(mod13).7^{12} \equiv 1 \pmod{13}.

Step 3 — Reduce the exponent modulo 12. Write 100=12⋅8+4100 = 12 \cdot 8 + 4, so

7100=(712)8⋅74≡18⋅74≡74(mod13).7^{100} = (7^{12})^{8}\cdot 7^{4} \equiv 1^8 \cdot 7^4 \equiv 7^4 \pmod{13}.

Step 4 — Evaluate. From Step 1, 74≡9(mod13)7^4 \equiv 9 \pmod{13}.

 7100≡9(mod13) \boxed{\,7^{100} \equiv 9 \pmod{13}\,}
ExampleFinding a Modular Inverse and Solving a Congruence

Given: Solve 5x≡3(mod11)5x \equiv 3 \pmod{11}.

Step 1 — Find the inverse of 5 modulo 11. We need 5y≡1(mod11)5y \equiv 1 \pmod{11}. Test values: 5⋅9=45=44+1≡15\cdot 9 = 45 = 44 + 1 \equiv 1. So 5−1≡9(mod11)5^{-1} \equiv 9 \pmod{11}.

Step 2 — Multiply both sides by the inverse.

9⋅5x≡9⋅3(mod11).9 \cdot 5x \equiv 9 \cdot 3 \pmod{11}.

Step 3 — Simplify the left side. Since 9⋅5≡19 \cdot 5 \equiv 1:

x≡27(mod11).x \equiv 27 \pmod{11}.

Step 4 — Reduce. 27=22+527 = 22 + 5, so 27≡5(mod11)27 \equiv 5 \pmod{11}.

 x≡5(mod11) \boxed{\,x \equiv 5 \pmod{11}\,}

Check: 5⋅5=25≡3(mod11)5 \cdot 5 = 25 \equiv 3 \pmod{11}. ✓

Common Misconceptions

⚠️ Common Errors
  • "a  na \bmod n is always positive in every language." Mathematically the remainder satisfies 0≤r<n0 \le r < n, but some programming languages (e.g., C, Java) return a negative result for negative inputs (e.g., −7  3-7 \bmod 3 may be −1-1 rather than 22). Always normalize.
  • Confusing the operator   \bmod with the relation ≡(modn)\equiv \pmod n. "17  5=217 \bmod 5 = 2" is a function returning a number; "17≡2(mod5)17 \equiv 2 \pmod 5" is a true/false statement about congruence.
  • Assuming inverses always exist. a−1a^{-1} modulo nn exists iff gcd⁡(a,n)=1\gcd(a,n)=1.
  • Cancelling carelessly. From ac≡bc(modn)ac \equiv bc \pmod n you may conclude a≡ba \equiv b only if gcd⁡(c,n)=1\gcd(c,n)=1.
CounterexampleCancellation Fails Without Coprimality

Consider (mod12)\pmod{12}:

3⋅2=6and9⋅2=18≡6(mod12).3 \cdot 2 = 6 \quad\text{and}\quad 9 \cdot 2 = 18 \equiv 6 \pmod{12}.

So 3⋅2≡9⋅2(mod12)3 \cdot 2 \equiv 9 \cdot 2 \pmod{12}, yet 3≢9(mod12)3 \not\equiv 9 \pmod{12}. The cancellation of the factor 2 is illegal because gcd⁡(2,12)=2≠1\gcd(2,12) = 2 \ne 1.

Connections

connection · How Modular Arithmetic Sits Among Its Neighbours

Congruence is built directly on the Division Algorithm (every integer has a unique remainder) and on the Greatest Common Divisor (which decides when inverses exist). The set Z/nZ\mathbb{Z}/n\mathbb{Z} with ++ and ×\times is the introductory student's first real encounter with a finite Ring (Algebra); when nn is prime it becomes a Finite Field. The key computational engine is the Extended Euclidean Algorithm, which produces inverses. Deeper structural results — the Chinese Remainder Theorem, Fermat's Little Theorem, and Euler's Theorem — all live inside this framework, and together they form the foundation of Public-Key Cryptography.

Real-World Applications

🔬 Where Modular Arithmetic Is Used

Historical Development

⏳ History
- c. 300 BCE: Euclid's Elements presents the algorithm for the Greatest Common Divisor, implicitly handling remainders.
- c. 3rd–5th century CE: Chinese mathematician Sun Tzu poses remainder problems in Sunzi Suanjing, the seed of the Chinese Remainder Theorem.
- 1247: Qin Jiushao systematizes the Chinese Remainder Theorem in Mathematical Treatise in Nine Sections.
- 1640: Pierre de Fermat states his "Little Theorem," ap≡a(modp)a^{p} \equiv a \pmod p for prime pp, in a letter (without proof).
- 1736: Leonhard Euler publishes the first proof of Fermat's Little Theorem and later generalizes it via the totient function φ(n)\varphi(n).
- 1801: Carl Friedrich Gauss publishes Disquisitiones Arithmeticae, introducing the "≡\equiv" notation and the formal theory of congruences.
- 1801: Gauss proves the law of quadratic reciprocity using congruence machinery.
- 1976: Whitfield Diffie and Martin Hellman publish their key-exchange protocol, igniting modern public-key cryptography.
- 1977: Rivest, Shamir, and Adleman invent RSA, making modular exponentiation a cornerstone of digital security.

Subtleties & Limits

open-problem · Computational Hardness and Open Questions

Modular arithmetic is computationally easy in the forward direction — multiplying and exponentiating modulo nn is fast. The deep difficulty lies in inverting these operations:

  • The Discrete Logarithm Problem (given gg, pp, and gx  pg^x \bmod p, find xx) is believed hard but has no proven lower bound — its difficulty underpins Diffie–Hellman and elliptic-curve cryptography.
  • The Integer Factorization Problem (recovering p,qp,q from n=pqn=pq) similarly has no known efficient classical algorithm, yet Shor's algorithm solves both on a hypothetical large-scale quantum computer in polynomial time, threatening current cryptosystems.
  • Whether efficient classical algorithms for these problems exist is tied to the broader, unresolved landscape of computational complexity (related to P\mathbf{P} vs NP\mathbf{NP}). No proof of hardness — only strong empirical confidence — currently exists.

See Also

Division Algorithm · Greatest Common Divisor · Extended Euclidean Algorithm · Modular Multiplicative Inverse · Fermat's Little Theorem · Euler's Theorem · Chinese Remainder Theorem · Finite Field · Ring (Algebra) · Public-Key Cryptography · Number Theory