Skip to main content

Modular Arithmetic

If it is 10 o'clock now and you wait 5 hours, the clock reads 3, not 15. You just did modular arithmetic — you divided by 12 and kept only the remainder. Modular arithmetic is the mathematics of remainders, and it turns out that this simple idea of "wrapping around" a fixed cycle is one of the most powerful tools in all of number theory. It is the machinery behind the check digit on every barcode and credit card, the RSA encryption that protects your online banking, the hash tables inside your programs, and the day-of-the-week calculations in every calendar.

The genius of modular arithmetic is that it lets you do ordinary addition, subtraction, and multiplication inside a finite world of just nn numbers, and everything stays consistent. Once you learn to think in "clock time" instead of the infinite number line, whole classes of problems that look terrifying — like the last digit of 7100 7^{100} — become almost trivial.

Learning Objectives

  • Understand what it means for two integers to be congruent modulo nn and read the notation fluently.
  • Compute sums, differences, and products in arithmetic mod nn, and know which operations are safe.
  • Find modular inverses and understand when they exist.
  • Use fast exponentiation to evaluate large powers modulo nn.
  • Explain how check-digit schemes (ISBN, UPC, Luhn) and public-key cryptography rely on modular arithmetic.
  • Distinguish congruence from equality and avoid the classic cancellation trap.

Quick Answer

Two integers aa and bb are congruent modulo nn, written ab(modn)a \equiv b \pmod{n}, when they leave the same remainder on division by nn — equivalently, when nn divides aba - b. The number nn is the modulus. Congruence respects addition, subtraction, and multiplication: if aba \equiv b and cdc \equiv d, then a+cb+da + c \equiv b + d and acbdac \equiv bd. This lets you replace any number by its remainder at any stage of a calculation. Division is the tricky part — you can only "divide" by multiplying by a modular inverse, which exists exactly when the divisor is coprime to nn. These rules power check digits, hashing, and modern cryptography.

Where It Came From

People had informally reasoned about remainders and cycles for millennia — Chinese mathematicians solved simultaneous remainder problems in the third century (the Chinese Remainder Theorem), and calendar-makers everywhere juggled 7-day weeks and 12-month years. But this was all ad hoc. There was no unified language, no algebra of remainders, and no theorems you could reuse.

That changed in 1801 when a 24-year-old Carl Friedrich Gauss published Disquisitiones Arithmeticae ("Investigations in Arithmetic"). The very first section introduces the congruence notation ab(modn)a \equiv b \pmod{n} that we still use today — Gauss deliberately chose the symbol \equiv because congruence behaves so much like equality. This was the crucial conceptual leap. By making "having the same remainder" into a relation you could manipulate with its own algebra, Gauss transformed a pile of scattered tricks into a systematic theory.

The need was real. Gauss and his predecessors (Fermat, Euler, Lagrange) were wrestling with deep questions: When does x2a(modp)x^2 \equiv a \pmod{p} have a solution? How do primes distribute? What are the solutions to polynomial equations in integers? These problems demanded a way to reason about integers "up to a remainder" cleanly and rigorously. The congruence notation made proofs shorter, patterns visible, and results composable. Disquisitiones went on to prove quadratic reciprocity and lay the foundations of algebraic number theory, and it is no exaggeration to say modern number theory begins on its first page. Two centuries later, when cryptographers needed hard mathematical problems to secure the internet, they reached straight for Gauss's arithmetic of remainders.

Congruence: The Core Idea

The formal definition is beautifully simple. For a positive integer nn:

ab(modn)meansn(ab) a \equiv b \pmod{n} \quad\text{means}\quad n \mid (a - b)

Read "n(ab)n \mid (a-b)" as "nn divides aba - b evenly." Equivalently, aa and bb have the same remainder when divided by nn.

Worked example. Is 3814(mod12) 38 \equiv 14 \pmod{12}? Compute 3814=24 38 - 14 = 24, and 24=12×2 24 = 12 \times 2, so 12 12 divides 24 24. Yes, they are congruent. Check the remainders directly: 38=12×3+2 38 = 12 \times 3 + 2 (remainder 2) and 14=12×1+2 14 = 12 \times 1 + 2 (remainder 2). Same remainder, confirming the congruence.

Every integer is congruent mod nn to exactly one of the values 0,1,2,,n1 0, 1, 2, \dots, n-1. These are the residues, and the whole set is written Zn={0,1,,n1}\mathbb{Z}_n = \{0, 1, \dots, n-1\}. When we "reduce mod nn," we replace a number by its residue. For example, 100mod7=2 100 \bmod 7 = 2 because 100=7×14+2 100 = 7 \times 14 + 2.

Congruence is an equivalence relation — it is reflexive (aaa \equiv a), symmetric (if aba \equiv b then bab \equiv a), and transitive (if aba \equiv b and bcb \equiv c then aca \equiv c). This is exactly why Gauss's \equiv symbol behaves like ==.

Arithmetic Mod n

The reason modular arithmetic is so useful is that congruence is compatible with the basic operations. If ab(modn)a \equiv b \pmod{n} and cd(modn)c \equiv d \pmod{n}, then:

  • a+cb+d(modn)a + c \equiv b + d \pmod{n}
  • acbd(modn)a - c \equiv b - d \pmod{n}
  • a×cb×d(modn)a \times c \equiv b \times d \pmod{n}

In practice this means you can reduce numbers whenever you like — before, during, or after a calculation — and the answer stays correct. This keeps numbers small.

Worked example (multiplication). Find 47×58mod9 47 \times 58 \bmod 9. Rather than computing 47×58=2726 47 \times 58 = 2726 and then dividing, reduce first: 472(mod9) 47 \equiv 2 \pmod 9 (since 47=45+2 47 = 45 + 2) and 584(mod9) 58 \equiv 4 \pmod 9 (since 58=54+4 58 = 54 + 4). So 47×582×4=8(mod9) 47 \times 58 \equiv 2 \times 4 = 8 \pmod 9. Check: 2726=9×302+8 2726 = 9 \times 302 + 8. Remainder 8. Correct.

Worked example (last digit of a big power). What is the last digit of 7100 7^{100}? The last digit is the value mod 10. Powers of 7 mod 10 cycle: 717 7^1 \equiv 7, 729 7^2 \equiv 9, 733 7^3 \equiv 3, 741 7^4 \equiv 1, then it repeats every 4. Since 100=4×25 100 = 4 \times 25, we have 7100=(74)25125=1(mod10) 7^{100} = (7^4)^{25} \equiv 1^{25} = 1 \pmod{10}. The last digit is 1.

Fast Modular Exponentiation

For genuinely large exponents, we use repeated squaring. To compute akmodn a^k \bmod n, square repeatedly and reduce at each step.

Worked example. Compute 313mod7 3^{13} \bmod 7. Write 13=8+4+1 13 = 8 + 4 + 1 in binary (11012 1101_2).

  • 313(mod7) 3^1 \equiv 3 \pmod 7
  • 3292(mod7) 3^2 \equiv 9 \equiv 2 \pmod 7
  • 3422=4(mod7) 3^4 \equiv 2^2 = 4 \pmod 7
  • 3842=162(mod7) 3^8 \equiv 4^2 = 16 \equiv 2 \pmod 7

Then 313=3834312×4×3=243(mod7) 3^{13} = 3^8 \cdot 3^4 \cdot 3^1 \equiv 2 \times 4 \times 3 = 24 \equiv 3 \pmod 7. This method needs only about log2k\log_2 k multiplications, which is what makes cryptography with 2048-bit numbers computationally feasible.

Division and Modular Inverses

Here is the crucial catch: division does not always work in modular arithmetic. You cannot naively cancel. The correct notion of division is multiplying by a modular inverse.

The inverse of aa mod nn is a number a1a^{-1} with aa11(modn)a \cdot a^{-1} \equiv 1 \pmod{n}. It exists if and only if gcd(a,n)=1\gcd(a, n) = 1 (that is, aa and nn are coprime).

Worked example. Find the inverse of 3 3 mod 7 7. We want 3x1(mod7) 3x \equiv 1 \pmod 7. Testing: 3×5=15=14+11(mod7) 3 \times 5 = 15 = 14 + 1 \equiv 1 \pmod 7. So 315(mod7) 3^{-1} \equiv 5 \pmod 7. To "divide by 3" mod 7, multiply by 5.

Why coprimality matters. Consider mod 6. There is no inverse of 2, because 2x 2x mod 6 only ever hits 0,2,4 0, 2, 4 — never 1. And gcd(2,6)=21\gcd(2,6) = 2 \neq 1. But when n=pn = p is prime, every nonzero residue has an inverse, which is why prime moduli are so beloved in cryptography.

Real-World Applications

  • Check digits (ISBN, UPC, IBAN). The 13-digit ISBN on every book ends in a check digit chosen so a weighted sum is 0(mod10)\equiv 0 \pmod{10}. If you mistype a digit, the sum breaks the congruence and the error is caught instantly.
  • Credit-card validation (Luhn algorithm). Every card number satisfies a mod-10 checksum. Before a payment form even contacts the bank, it uses modular arithmetic to reject typos.
  • Cryptography (RSA). RSA encrypts a message mm as cme(modn)c \equiv m^e \pmod n, where nn is a product of two enormous primes. Decryption uses the inverse exponent mod φ(n)\varphi(n). Security rests on the difficulty of factoring nn — pure Gaussian arithmetic scaled to hundreds of digits.
  • Hashing and data structures. Hash tables map a key kk to a bucket kmodnk \bmod n, spreading data evenly across memory.
  • Calendars and scheduling. Day-of-week is a mod-7 calculation; recurring events cycle through mod arithmetic.
  • Random number generators. Linear congruential generators produce pseudo-random sequences via xn+1(axn+c)(modm) x_{n+1} \equiv (a x_n + c) \pmod m.

Worked ISBN example. An ISBN-13 uses weights 1,3,1,3, 1, 3, 1, 3, \dots. Take a number starting 978030640615 978030640615 with unknown check digit dd. The weighted sum of the first 12 digits (weights alternating 1 and 3) plus dd must be 0(mod10)\equiv 0 \pmod{10}. Computing 91+73+81+03+31+03+61+43+01+63+11+53=9+21+8+0+3+0+6+12+0+18+1+15=93 9\cdot1 + 7\cdot3 + 8\cdot1 + 0\cdot3 + 3\cdot1 + 0\cdot3 + 6\cdot1 + 4\cdot3 + 0\cdot1 + 6\cdot3 + 1\cdot1 + 5\cdot3 = 9+21+8+0+3+0+6+12+0+18+1+15 = 93. We need 93+d0(mod10) 93 + d \equiv 0 \pmod{10}, so d937(mod10) d \equiv -93 \equiv 7 \pmod{10}. The check digit is 7.

Common Mistakes

  1. Confusing congruence with equality. Writing 175(mod12) 17 \equiv 5 \pmod{12} does not mean 17=5 17 = 5. It means they share a remainder. Congruence is a relation between numbers, not a claim that they are the same number. Always carry the "(modn)\pmod n" label.

  2. Cancelling illegally. From 2×32×0(mod6) 2 \times 3 \equiv 2 \times 0 \pmod 6 (both sides are 0) you might try to cancel the 2 to get 30(mod6) 3 \equiv 0 \pmod 6, which is false. You may only cancel a factor that is coprime to the modulus. Since gcd(2,6)=2\gcd(2,6) = 2, cancelling 2 is illegal here.

  3. Assuming division always works. Students write "x4/2(modn)x \equiv 4/2 \pmod n" as if fractions exist. There are no fractions mod nn; you must use a modular inverse, which only exists when the divisor is coprime to nn. Check the gcd first.

  4. Mishandling negatives. 1mod5 -1 \bmod 5 is 4 4, not 1-1 or 1 1. A residue should be taken in the range 0 0 to n1n-1. Add the modulus until you land in range: 1+5=4 -1 + 5 = 4.

Comparison and Connections

Modular arithmetic sits at the crossroads of division, group theory, and cryptography. It helps to see how the related ideas line up.

ConceptWhat it capturesKey condition
Divisibility (nan \mid a)aa leaves remainder 0remainder is exactly 0
Congruence (aba \equiv b)a,ba, b share a remaindern(ab)n \mid (a - b)
Modular inverse"division" mod nngcd(a,n)=1\gcd(a,n) = 1
Chinese Remainder Theoremcombining several modulimoduli pairwise coprime
Fermat's Little Theoremap11(modp)a^{p-1} \equiv 1 \pmod ppp prime, pap \nmid a

The set Zn\mathbb{Z}_n with addition forms a group, and the coprime residues under multiplication form a group too — modular arithmetic is many students' first encounter with abstract algebra. Fermat's Little Theorem and its generalization Euler's theorem are what make RSA's key math work, and both are statements about modular exponentiation.

Practice Questions

Recall

State the definition of ab(modn)a \equiv b \pmod n in two equivalent ways.

Answer: (1) nn divides aba - b; (2) aa and bb have the same remainder when divided by nn.

Understanding

Compute 85mod7 85 \bmod 7 and 13mod7 -13 \bmod 7.

Answer: 85=7×12+1 85 = 7 \times 12 + 1, so 851(mod7) 85 \equiv 1 \pmod 7. For 13 -13: add 14 to get 1, so 131(mod7) -13 \equiv 1 \pmod 7. (Both equal 1.)

Application

Find the last two digits of 320 3^{20}.

Guidance: Work mod 100. 310=5904949(mod100) 3^{10} = 59049 \equiv 49 \pmod{100}. Then 320=(310)2492=24011(mod100) 3^{20} = (3^{10})^2 \equiv 49^2 = 2401 \equiv 1 \pmod{100}. The last two digits are 01.

Analysis

Explain why 4x2(mod6) 4x \equiv 2 \pmod 6 has solutions but 4x3(mod6) 4x \equiv 3 \pmod 6 has none.

Guidance: gcd(4,6)=2\gcd(4,6) = 2. A congruence axb(modn) ax \equiv b \pmod n is solvable iff gcd(a,n)\gcd(a,n) divides bb. Since 22 2 \mid 2, the first has solutions (x=2x = 2 and x=5x = 5). Since 23 2 \nmid 3, the second is unsolvable — 4x 4x mod 6 is always even, never 3.

FAQ

Is "mod" an operation or a relation? Both, and it confuses everyone at first. "amodna \bmod n" (the operation) returns the remainder, a single number in 0 0 to n1n-1. "ab(modn)a \equiv b \pmod n" (the relation) says two numbers share that remainder. Context and notation tell them apart.

Why does the clock use mod 12 but the military uses mod 24? They are just different moduli for the same idea. A 12-hour clock wraps every 12 hours; a 24-hour clock every 24. Both are modular arithmetic on the hours.

Can the modulus be negative or zero? By convention the modulus is a positive integer (at least 1, and usually at least 2 to be interesting). Mod 0 would mean no wrapping — ordinary equality. Negative moduli add nothing new since mod(n)\bmod (-n) behaves like modn\bmod\, n.

Do calculators do modular arithmetic? Most have a "mod" function or you can compute ana/n a - n \lfloor a/n \rfloor. Programming languages have a % operator, though be careful: some languages return negative results for negative inputs, unlike the standard math convention.

Why is prime modulus special in cryptography? When nn is prime, every nonzero residue has a multiplicative inverse, so Zp\mathbb{Z}_p behaves like a "field" — you can divide freely. This clean structure supports Fermat's Little Theorem, Diffie-Hellman key exchange, and much of modern cryptography.

Quick Revision

  • ab(modn)a \equiv b \pmod n means n(ab)n \mid (a - b) — same remainder.
  • Congruence respects ++, -, ×\times; reduce mod nn anytime to keep numbers small.
  • Residues: every integer \equiv exactly one of 0,,n1 0, \dots, n-1.
  • Inverse a1a^{-1} exists iff gcd(a,n)=1\gcd(a, n) = 1; division = multiply by inverse.
  • axb(modn) ax \equiv b \pmod n solvable iff gcd(a,n)b\gcd(a,n) \mid b.
  • Fast powers: repeated squaring, reduce each step.
  • Negatives: add the modulus to land in 0 0 to n1n-1.
  • Applications: check digits (mod 10), RSA (mod nn), hashing (mod table size).

Prerequisites

  • Divisibility and the Euclidean algorithm (finds gcd and inverses)
  • Prime numbers and factorization (the basis of RSA security)

Next Topics

  • Fermat's Little Theorem and Euler's theorem
  • The Chinese Remainder Theorem
  • Introduction to cryptography and RSA