MathLabs

Arithmetic and number theory

Congruences and modular arithmetic

Arithmetic where numbers wrap around after reaching a fixed modulus.

IntuitionThe clock: numbers that wrap around

Look at a clock face. After 1212 o'clock comes 11 again, not 1313: the hours "wrap around" every 1212 steps. If it is 99 o'clock now, 88 hours later it is not "1717 o'clock" but 55 o'clock, because 1717 and 55 leave the same remainder when divided by 1212. This everyday wrapping is exactly the idea behind congruences: two integers are treated as "the same" if they differ by a multiple of a fixed number mm, called the modulus. Congruences let us replace huge numbers by their remainders and still compute correctly — the engine behind clocks, calendars, check digits, hash tables, and modern cryptography.

A point moving around a circle divided into equal arcs, illustrating how remainders modulo m cycle back to the start.
On the modulo-mm clock, chords connect each residue xx to ax mod ma x \bmod m. Change mm and aa to watch when the map permutes all residues vs collapses onto a subgroup.

SchoolDefinition and basic properties

Definition: Congruence modulo mm

Fix a positive integer mm (the modulus). Two integers aa and bb are **congruent modulo mm**, written a≡b(modm)a \equiv b \pmod{m}, when mm divides their difference: m∣(a−b)m \mid (a-b). Equivalently, aa and bb leave the same remainder when divided by mm. All integers congruent to a fixed aa form its residue class [a]={…,a−m,a,a+m,a+2m,… }[a] = \{\dots, a-m, a, a+m, a+2m, \dots\}, and there are exactly mm distinct residue classes, written Z/mZ\mathbb{Z}/m\mathbb{Z}.

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

Here a,b,ma,b,m are integers with m>0m>0; the symbol ∣\mid reads "divides", and (modm)\pmod m names the modulus attached to a congruence. This single definition already tells us that congruence is reflexive, symmetric, and transitive — an equivalence relation — which is why it makes sense to talk about "the residue classes modulo mm" as objects in their own right, collected into the set Z/mZ={ [0],[1],…,[m−1] }\mathbb{Z}/m\mathbb{Z} = \{\,[0],[1],\dots,[m-1]\,\}.

Z/mZ={ [0], [1], …, [m−1] }\mathbb{Z}/m\mathbb{Z} = \{\,[0],\,[1],\,\dots,\,[m-1]\,\}
Which operations respect congruence?
OperationRule (if a≡b, c≡d(modm)a\equiv b,\ c\equiv d \pmod m)Numeric example (mod 1212)
Additiona+c≡b+d(modm)a+c\equiv b+d \pmod m9+8≡21≡9(mod12)9+8\equiv 21\equiv 9 \pmod{12}
Subtractiona−c≡b−d(modm)a-c\equiv b-d \pmod m2−5≡−3≡9(mod12)2-5\equiv -3\equiv 9 \pmod{12}
Multiplicationac≡bd(modm)ac\equiv bd \pmod m5×5≡25≡1(mod12)5\times 5\equiv 25\equiv 1 \pmod{12}
Division (cancel cc)valid only if gcd⁡(c,m)=1\gcd(c,m)=14×2≡4×5(mod6)4\times 2\equiv 4\times 5\pmod 6 but 2≢52\not\equiv 5

UndergraduateTheorems

For a fixed modulus mm: (i) a≡a(modm)a \equiv a \pmod{m} for every aa; (ii) a≡b(modm)  ⟹  b≡a(modm)a\equiv b \pmod m \implies b\equiv a\pmod m; (iii) a≡b, b≡c(modm)  ⟹  a≡c(modm)a\equiv b,\ b\equiv c \pmod m \implies a\equiv c \pmod m; and if a≡b(modm)a\equiv b \pmod m and c≡d(modm)c\equiv d\pmod m then a+c≡b+d(modm)a+c \equiv b+d \pmod{m} and ac≡bd(modm)ac \equiv bd \pmod{m}.

Why is it true?

This is what makes congruences usable as arithmetic: it means we can add, subtract, and multiply remainders directly instead of the huge original numbers, and always get the correct remainder back — the reason computers can check a 1616-digit card number or compute 7100 mod 137^{100} \bmod 13 without ever storing a 100100-digit number.

Proof

Reflexivity: a−a=0a-a=0 and m∣0m\mid 0 for every mm, so a≡a(modm)a\equiv a\pmod m.

Symmetry: if a≡b(modm)a\equiv b\pmod m then a−b=mka-b=mk for some integer kk, so b−a=m(−k)b-a=m(-k); since −k-k is also an integer, m∣(b−a)m\mid(b-a), i.e. b≡a(modm)b\equiv a\pmod m.

Transitivity: if a≡b(modm)a\equiv b\pmod m and b≡c(modm)b\equiv c\pmod m, write a−b=mk1a-b=mk_1 and b−c=mk2b-c=mk_2. Adding these, a−c=(a−b)+(b−c)=m(k1+k2)a-c=(a-b)+(b-c)=m(k_1+k_2), so m∣(a−c)m\mid(a-c) and a≡c(modm)a\equiv c\pmod m.

Compatibility with addition: from a−b=mk1a-b=mk_1 and c−d=mk2c-d=mk_2, add the two equations: (a+c)−(b+d)=m(k1+k2)(a+c)-(b+d) = m(k_1+k_2), which is a multiple of mm, so a+c≡b+d(modm)a+c\equiv b+d\pmod m.

Compatibility with multiplication: write ac−bd=ac−bc+bc−bd=c(a−b)+b(c−d)=c⋅mk1+b⋅mk2=m(ck1+bk2)ac-bd = ac-bc+bc-bd = c(a-b)+b(c-d) = c\cdot mk_1 + b\cdot mk_2 = m(ck_1+bk_2). This is again a multiple of mm, so ac≡bd(modm)ac\equiv bd\pmod m. Together, (i)-(iii) make congruence an equivalence relation, and the last two steps show every ring operation on Z\mathbb{Z} descends to a well-defined operation on residue classes Z/mZ\mathbb{Z}/m\mathbb{Z}.

If gcd⁡(c,m)=1\gcd(c,m)=1, then ac≡bc(modm), gcd⁡(c,m)=1  ⟹  a≡b(modm)ac\equiv bc \pmod m,\ \gcd(c,m)=1 \implies a\equiv b \pmod m. Equivalently, cc has a multiplicative inverse modulo mm: an integer uu with cu≡1(modm)cu\equiv 1\pmod m.

Why is it true?

Ordinary "division" is really multiplication by an inverse, and this theorem tells us exactly when that inverse exists modulo mm: precisely when cc shares no common factor with mm. Without this condition cancellation genuinely fails (see the last row of the table above), which is why every later result about powers modulo mm — Fermat's little theorem, Euler's theorem, the Chinese remainder theorem — is built on top of this single algebraic fact.

Proof

Since gcd⁡(c,m)=1\gcd(c,m)=1, Bézout's identity (a consequence of the Euclidean algorithm) guarantees ∃ u,v∈Z: cu+mv=1\exists\, u,v \in \mathbb{Z}:\ cu+mv=1: there exist integers u,vu,v with cu+mv=1cu+mv=1.

Multiply both sides of ac≡bc(modm)ac\equiv bc\pmod m by uu: acu≡bcu(modm)acu\equiv bcu\pmod m. Now substitute cu=1−mvcu=1-mv: the left side becomes a(1−mv)=a−amva(1-mv)=a-amv, and since amvamv is a multiple of mm, a(1−mv)≡a(modm)a(1-mv)\equiv a\pmod m; likewise the right side b(1−mv)≡b(modm)b(1-mv)\equiv b\pmod m. Hence a≡b(modm)a\equiv b\pmod m, proving cancellation.

Moreover, taking a=1,b=0,c=ca=1,b=0,c=c is not needed — directly, cu=1−mv≡1(modm)cu = 1-mv \equiv 1 \pmod m by the same substitution, so uu itself is the multiplicative inverse of cc modulo mm: this is exactly the object whose existence Theorem statement claims.

The hypothesis gcd⁡(c,m)=1\gcd(c,m)=1 is essential: take c=4, m=6c=4,\ m=6 (so gcd⁡(4,6)=2≠1\gcd(4,6)=2\neq1). Then 4×2=8≡2(mod6)4\times 2=8\equiv 2\pmod 6 and 4×5=20≡2(mod6)4\times 5=20\equiv 2\pmod 6, so 4×2≡4×5(mod6)4\times2\equiv 4\times5\pmod 6, yet 2≢5(mod6)2\not\equiv 5\pmod 6 — cancellation genuinely fails once the coprimality hypothesis is dropped.

AdvancedReal-World Applications and Worked Examples

Modular arithmetic is one of the most quietly ubiquitous tools in applied mathematics. Calendars use it to find the day of the week (mod 77); barcode and ISBN systems use it to catch typing errors with a single check digit (mod 1111 or mod 1010); hash tables in computer science map keys to buckets by taking a remainder; and RSA-style cryptography (covered in depth in the next two topics) encodes and decodes messages entirely through modular exponentiation. Two concrete examples below show the everyday reasoning behind the calendar and the check-digit applications.

Example: Finding the day of the week

If today is Tuesday, what day of the week will it be 100100 days from now?

Solution

The days of the week repeat with period 77, so only the remainder of 100100 upon division by 77 matters: 100=7×14+2100 = 7\times 14 + 2, so 100≡2(mod7)100 \equiv 2 \pmod 7.

By the additive compatibility proved above, moving forward 100100 days has the same effect modulo 77 as moving forward just 22 days: Tuesday + 2+\,2 days is Thursday.

So 100100 days from a Tuesday is a Thursday. Notice that we never had to count all 100100 days one by one — congruence let us collapse a large number into its small, equivalent remainder.

Example: Verifying an ISBN-10 check digit

An ISBN-10 code d1d2⋯d10d_1d_2\cdots d_{10} is valid exactly when ∑i=110i di≡0(mod11)\sum_{i=1}^{10} i\, d_i \equiv 0 \pmod{11}. Check whether 0-306-40615-20\text{-}306\text{-}40615\text{-}2 is a valid ISBN-10.

Solution

List the digits d1,…,d10=0,3,0,6,4,0,6,1,5,2d_1,\dots,d_{10} = 0,3,0,6,4,0,6,1,5,2 and form the weighted sum ∑i di=1(0)+2(3)+3(0)+4(6)+5(4)+6(0)+7(6)+8(1)+9(5)+10(2)\sum i\,d_i = 1(0)+2(3)+3(0)+4(6)+5(4)+6(0)+7(6)+8(1)+9(5)+10(2).

Compute each term: 0,6,0,24,20,0,42,8,45,200,6,0,24,20,0,42,8,45,20. Adding them: 0+6+0+24+20+0+42+8+45+20=1650+6+0+24+20+0+42+8+45+20 = 165.

By the multiplicative and additive compatibility of congruence, we only need 165 mod 11165 \bmod 11: since 11×15=16511\times 15 = 165, we get 165≡0(mod11)165 \equiv 0 \pmod{11}. The check passes, so 0-306-40615-20\text{-}306\text{-}40615\text{-}2 is a valid ISBN-10. If a single digit were mistyped, the weighted sum would almost certainly fail to be ≡0(mod11)\equiv 0 \pmod{11}, which is exactly why publishers use this scheme to catch data-entry errors automatically.

ResearchCongruences at the research frontier: residue systems in modern cryptography

Using the standard convention 0≤r<m0 \le r < m, what is −17 mod 5-17 \bmod 5?

If a≡4(mod9)a \equiv 4 \pmod 9 and b≡7(mod9)b \equiv 7 \pmod 9, what is ab mod 9ab \bmod 9?

If today is Tuesday, what day of the week is 5050 days from now?

The cancellation law ac≡bc(modm)  ⟹  a≡b(modm)ac\equiv bc \pmod m \implies a\equiv b\pmod m is guaranteed to hold when:

References

  1. Carl Friedrich Gauss (1801). Disquisitiones Arithmeticae
  2. Jung Hee Cheon, Andrey Kim, Miran Kim, Yongsoo Song (2017). Homomorphic Encryption for Arithmetic of Approximate Numbers · DOI:10.1007/978-3-319-70694-8_15