Modular Arithmetic and Congruences

⚡ Key Takeaways

Modular arithmetic is the arithmetic of remainders: integers are considered equal when they differ by a multiple of a fixed modulus nn. Key result: the integers modulo nn form a commutative ring Z/nZ\mathbb{Z}/n\mathbb{Z}, and the nonzero residues form a group under multiplication exactly when nn is prime. It applies wherever cyclic or periodic structure appears — cryptography, hashing, calendars, error correction, number theory. Formalized by Carl Friedrich Gauss in Disquisitiones Arithmeticae (1801).

💡 The Core Insight

Fix a modulus nn. Declare two integers equivalent when they leave the same remainder upon division by nn. Because this equivalence is compatible with ++ and ×\times — equal inputs give equal outputs — the entire arithmetic of Z\mathbb{Z} descends to a self-contained arithmetic on just nn symbols {0,1,…,n−1}\{0,1,\dots,n-1\}. Everything else (units, inverses, the Chinese Remainder Theorem, Fermat's and Euler's theorems) is a consequence of this compatibility.

Formal Statement

DefinitionCongruence Modulo n

Let n∈Z>0n \in \mathbb{Z}_{>0} be the modulus. For a,b∈Za, b \in \mathbb{Z}, we say aa is congruent to bb modulo nn, written a≡b(modn)a \equiv b \pmod{n}, precisely when n∣(a−b)n \mid (a - b):

1
a≡b(modn)    ⟺    ∃ k∈Z such that a−b=kn.\quad a \equiv b \pmod{n} \;\iff\; \exists\, k \in \mathbb{Z}\ \text{such that}\ a - b = kn.

Derivation hint: "nn divides a−ba-b" unwinds by the definition of divisibility into the existence of an integer multiplier kk. Here a,ba,b are integers, nn is the positive modulus, and kk is the integer witnessing divisibility.

DefinitionResidue Classes and the Ring Z/nZ\mathbb{Z}/n\mathbb{Z}

The residue class (or congruence class) of aa is the set

2
[a]n={ x∈Z:x≡a(modn) }={ a+kn:k∈Z }.\quad [a]_n = \{\, x \in \mathbb{Z} : x \equiv a \pmod{n}\,\} = \{\, a + kn : k \in \mathbb{Z}\,\}.

Derivation hint: collect every integer congruent to aa; by [1] these are exactly the integers a+kna + kn. The set of all nn distinct classes is Z/nZ={[0],[1],…,[n−1]}\mathbb{Z}/n\mathbb{Z} = \{[0],[1],\dots,[n-1]\}, equipped with the operations

3
[a]+[b]=[a+b],[a]⋅[b]=[a b].\quad [a] + [b] = [a+b], \qquad [a]\cdot[b] = [a\,b].

Derivation hint: define operations on representatives; well-definedness (independence of choice of representative) is proved below.

TheoremStructure of Z/nZ\mathbb{Z}/n\mathbb{Z}

(Z/nZ,+,⋅)(\mathbb{Z}/n\mathbb{Z}, +, \cdot) is a commutative ring with identity [1][1]. An element [a][a] has a multiplicative inverse if and only if gcd⁡(a,n)=1\gcd(a,n)=1. Consequently:

4
Z/nZ is a field  ⟺  n is prime.\quad \mathbb{Z}/n\mathbb{Z}\ \text{is a field} \iff n\ \text{is prime}.

Derivation hint: a finite commutative ring is a field iff every nonzero element is a unit; units are exactly the coprime residues (Bézout), and all nonzero residues are coprime to nn exactly when nn is prime. The group of units is

5
(Z/nZ)×={ [a]:gcd⁡(a,n)=1 },∣(Z/nZ)×∣=φ(n),\quad (\mathbb{Z}/n\mathbb{Z})^{\times} = \{\,[a] : \gcd(a,n)=1\,\}, \qquad \bigl|(\mathbb{Z}/n\mathbb{Z})^{\times}\bigr| = \varphi(n),

Derivation hint: collect the invertible classes; counting the coprime residues in {1,…,n}\{1,\dots,n\} is the definition of Euler's totient φ(n)\varphi(n). where φ\varphi is Euler's totient function.

TheoremChinese Remainder Theorem

If n=n1n2⋯nrn = n_1 n_2 \cdots n_r with the nin_i pairwise coprime, then there is a ring isomorphism

6
Z/nZ  ≅  Z/n1Z×Z/n2Z×⋯×Z/nrZ.\quad \mathbb{Z}/n\mathbb{Z} \;\cong\; \mathbb{Z}/n_1\mathbb{Z} \times \mathbb{Z}/n_2\mathbb{Z} \times \cdots \times \mathbb{Z}/n_r\mathbb{Z}.

Derivation hint: the map [x]↦([x]n1,…,[x]nr)[x]\mapsto([x]_{n_1},\dots,[x]_{n_r}) is a ring homomorphism; injectivity follows from pairwise coprimality and surjectivity from equal finite cardinalities.

Why It Holds

ProofWell-Definedness and the Unit Criterion

Step 1 — Congruence is an equivalence relation. Reflexivity: n∣0=a−an\mid 0=a-a. Symmetry: if n∣(a−b)n\mid(a-b) then n∣(b−a)n\mid(b-a). Transitivity: if n∣(a−b)n\mid(a-b) and n∣(b−c)n\mid(b-c), then n∣(a−b)+(b−c)=a−cn\mid(a-b)+(b-c)=a-c. Hence Z\mathbb{Z} partitions into the classes [a]n[a]_n.

Step 2 — Operations are well-defined. Suppose a≡a′a\equiv a' and b≡b′(modn)b\equiv b'\pmod n, so a−a′=kna-a'=kn, b−b′=ℓnb-b'=\ell n. Then (a+b)−(a′+b′)=(k+ℓ)n(a+b)-(a'+b') = (k+\ell)n, giving a+b≡a′+b′a+b\equiv a'+b'. For multiplication,

ab−a′b′=ab−a′b+a′b−a′b′=(a−a′)b+a′(b−b′)=(kb+a′ℓ)n,ab - a'b' = ab - a'b + a'b - a'b' = (a-a')b + a'(b-b') = (kb + a'\ell)n,

so ab≡a′b′ab\equiv a'b'. Thus [3] does not depend on representatives.

Step 3 — Ring axioms. Associativity, commutativity, and distributivity for +,⋅+,\cdot are inherited directly from Z\mathbb{Z} via the representatives. The class [0][0] is the additive identity, [−a][-a] the additive inverse, and [1][1] the multiplicative identity.

Step 4 — Units are coprime residues. [a][a] is a unit iff ∃x\exists x with ax≡1(modn)ax\equiv 1\pmod n, i.e. ax+ny=1ax + ny = 1 for some y∈Zy\in\mathbb{Z}. By Bézout's identity this is solvable iff gcd⁡(a,n)=1\gcd(a,n)=1. This proves [5], and since every nonzero residue is coprime to nn exactly when nn is prime, [4] follows. ■\blacksquare

∎
mathematician · Carl Friedrich Gauss | 1777–1855

Gauss introduced the congruence symbol "≡\equiv" and the systematic theory of residues in his 1801 masterwork Disquisitiones Arithmeticae, written when he was in his early twenties. He recognized that treating integers "up to multiples of nn" turned scattered number-theoretic observations — quadratic reciprocity, primitive roots, sums of squares — into a unified algebraic discipline. His notation and viewpoint are still exactly what every student learns today.

Building Intuition

🧠 Intuition

Think of a clock. After 12 hours the hour hand returns home: 13≡1(mod12)13 \equiv 1 \pmod{12}. The set of "clock positions" is finite, yet you can still add ("3 hours after 11 o'clock is 2 o'clock") and the arithmetic is consistent. Modular arithmetic generalizes this to any modulus: you are doing ordinary arithmetic but wrapping around a circle of nn slots. The deep payoff is that this wrapping is compatible with ++ and ×\times, so you never have to "leave the circle" to compute. Division is the subtle part — you can only undo multiplication by a number that shares no common factor with nn, because otherwise multiplying collapses distinct slots together (e.g. mod 6, 2⋅0=2⋅3=02\cdot 0 = 2\cdot 3 = 0).

Figure
The clock model: integers wrapping around a circle of n = 12 residues
0 1 2 3 4 5 6 7 8 9 10 11 13 ≡ 1 (mod 12)

Worked Examples

ExampleSolving a Linear Congruence

Given: Solve 14x≡30(mod100)14x \equiv 30 \pmod{100}.

Step 1 — Check solvability. d=gcd⁡(14,100)=2d=\gcd(14,100)=2. Since 2∣302 \mid 30, solutions exist, and there are exactly d=2d=2 of them mod 100.

Step 2 — Reduce by dd. Divide through (including the modulus) by 2:

7x≡15(mod50).7x \equiv 15 \pmod{50}.

Step 3 — Invert 77 mod 5050. Run the extended Euclidean algorithm: 50=7⋅7+150 = 7\cdot 7 + 1, so 1=50−7⋅71 = 50 - 7\cdot 7, giving 7−1≡−7≡43(mod50)7^{-1} \equiv -7 \equiv 43 \pmod{50}.

Step 4 — Solve. x≡43⋅15=645≡645−12⋅50=45(mod50)x \equiv 43 \cdot 15 = 645 \equiv 645 - 12\cdot 50 = 45 \pmod{50}.

Step 5 — Lift to mod 100. The two solutions mod 100 are 4545 and 45+50=9545 + 50 = 95.

x≡45 or 95(mod100)\boxed{x \equiv 45 \ \text{or}\ 95 \pmod{100}}
ExampleChinese Remainder Theorem in Action

Given: Find xx with x≡2(mod3)x \equiv 2 \pmod 3, x≡3(mod5)x \equiv 3 \pmod 5, x≡2(mod7)x \equiv 2 \pmod 7.

Step 1 — Modulus. n=3⋅5⋅7=105n = 3\cdot 5\cdot 7 = 105. Set N1=35N_1 = 35, N2=21N_2 = 21, N3=15N_3 = 15.

Step 2 — Partial inverses. 35≡2(mod3)⇒35−1≡235 \equiv 2 \pmod 3 \Rightarrow 35^{-1}\equiv 2. 21≡1(mod5)⇒21−1≡121 \equiv 1 \pmod 5 \Rightarrow 21^{-1}\equiv 1. 15≡1(mod7)⇒15−1≡115 \equiv 1 \pmod 7 \Rightarrow 15^{-1}\equiv 1.

Step 3 — Combine.

x≡2⋅35⋅2+3⋅21⋅1+2⋅15⋅1=140+63+30=233(mod105).x \equiv 2\cdot 35\cdot 2 + 3\cdot 21\cdot 1 + 2\cdot 15\cdot 1 = 140 + 63 + 30 = 233 \pmod{105}.

Step 4 — Reduce. 233−2⋅105=23233 - 2\cdot 105 = 23. Check: 23≡2 (3)23\equiv 2\,(3), 23≡3 (5)23\equiv 3\,(5), 23≡2 (7)23\equiv 2\,(7). ✓

x≡23(mod105)\boxed{x \equiv 23 \pmod{105}}

Common Misconceptions

⚠️ Common mistake

You cannot always cancel. From ac≡bc(modn)ac \equiv bc \pmod n you may not conclude a≡b(modn)a\equiv b\pmod n. The correct cancellation law is

ac≡bc(modn)  ⟹  a≡b(modn/gcd⁡(c,n)).ac\equiv bc \pmod n \implies a\equiv b \pmod{n/\gcd(c,n)}.

Cancellation is unrestricted only when gcd⁡(c,n)=1\gcd(c,n)=1. Likewise, do not reduce exponents mod nn: to simplify ak(modn)a^k \pmod n you reduce kk modulo φ(n)\varphi(n) (when gcd⁡(a,n)=1\gcd(a,n)=1), not modulo nn.

Counterexample

Mod 6: take a=1a=1, b=4b=4, c=2c=2. Then ac=2ac = 2 and bc=8≡2(mod6)bc = 8 \equiv 2 \pmod 6, so ac≡bcac\equiv bc. But a=1≢4=b(mod6)a=1 \not\equiv 4 = b \pmod 6. Cancellation fails because gcd⁡(2,6)=2≠1\gcd(2,6)=2\neq 1 — indeed the correct conclusion is only 1≡4(mod6/2=3)1\equiv 4 \pmod{6/2=3}, which is true. This also shows Z/6Z\mathbb{Z}/6\mathbb{Z} has zero divisors: [2]⋅[3]=[0][2]\cdot[3]=[0] with neither factor zero, so it is not a field.

Connections

connection

Modular arithmetic is the gateway to Group Theory: (Z/nZ,+)(\mathbb{Z}/n\mathbb{Z},+) is the prototypical finite cyclic group, and (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times is a finite abelian group whose order is φ(n)\varphi(n). The field criterion [4] connects it to Finite Fields, since Z/pZ\mathbb{Z}/p\mathbb{Z} is the field Fp\mathbb{F}_p. The CRT [6] is the arithmetic shadow of the ring-theoretic Direct Product of Rings. Through Fermat's Little Theorem and Euler's Theorem it powers exponentiation results, and via Quadratic Reciprocity it controls when residues are squares. The whole construction Z/nZ=Z/(n)\mathbb{Z}/n\mathbb{Z} = \mathbb{Z}/(n) is the quotient of Z\mathbb{Z} by an ideal, linking it to Ideals and Quotient Rings.

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- c. 300 BCE — Euclid gives the algorithm for gcd⁡\gcd in Elements Book VII, the computational backbone of inverses.
- c. 3rd century CE — Sun Zi poses the prototype "Chinese Remainder" problem in Sunzi Suanjing.
- 1247 — Qin Jiushao gives a general method (Da Yan Shu) for simultaneous remainder problems.
- 1640 — Pierre de Fermat states what is now Fermat's Little Theorem in a letter to Frénicle.
- 1736 — Leonhard Euler publishes the first proof of Fermat's theorem and later (1763) generalizes it via the totient φ\varphi.
- 1801 — Carl Friedrich Gauss introduces the "≡\equiv" notation and the systematic theory of congruences in Disquisitiones Arithmeticae.
- 1832 — Gauss extends congruences to the Gaussian integers, foreshadowing congruences in general rings.
- 1801–1900s — Dirichlet, Dedekind, and Kronecker recast congruences in the language of ideals and quotient rings.
- 1977 — Rivest, Shamir, Adleman publish RSA, making modular exponentiation the foundation of modern public-key cryptography.

Subtleties & Limits

open-problem

While modular arithmetic itself is fully understood, the computational questions it raises sit at the frontier. Finding discrete logarithms in (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times and factoring a modulus n=pqn=pq are believed hard for classical computers but have no proven super-polynomial lower bound — their difficulty is assumed, not established. Shor's quantum algorithm solves both in polynomial time, so the security of nearly all deployed public-key systems hinges on the unresolved status of large-scale quantum computing. Separately, the distribution of primitive roots and the smallest quadratic non-residue mod pp are tied to deep conjectures (Artin's conjecture, the Generalized Riemann Hypothesis) that remain open.

See Also

Euler's Totient Function · Chinese Remainder Theorem · Primitive Roots · Discrete Logarithm Problem · Cyclic Groups · Ideals and Quotient Rings