Search

Search papers and lessons

Modular Arithmetic

Level 1 · 8 min read

Arithmetic on a clock: numbers wrap around after a fixed size n, so two numbers are equal mod n when they leave the same remainder.

On this page

TL;DR

Modular arithmetic is counting that wraps around. You pick a size nn, and after you reach nn you start again from 00, the way a clock goes from 12 back to 1. Two whole numbers are congruent mod nn when they leave the same remainder after division by nn, and you can add, subtract and multiply while only keeping track of remainders.

Where you'll see it: in clock times and days of the week, in check digits on barcodes and bank cards, in hash tables, and in the modular exponentiation that secures most public-key cryptography.

Intuition

A clock with no hours beyond 12

Look at a clock. Three hours after 11 o'clock is 2 o'clock, not 14. The hour hand keeps going but the names repeat every twelve steps. We do not care how many full laps the hand has made. We only care where it points. That is the core idea: throw away the number of laps and keep the leftover position.

Mathematicians write this as "14≡2(mod12)14 \equiv 2 \pmod{12}", read "14 is congruent to 2 mod 12". The symbol ≡\equiv means "same position on the clock". The number 1212 is called the modulus.

Why does this idea exist? Many real problems only care about a position in a repeating cycle: the day of the week, the angle of a rotating part, the slot in a fixed-size table, the last digit of a long product. Working with remainders keeps numbers small and makes repeating patterns visible. It also turns out to be the right tool for building codes that detect typing errors and for hiding secrets behind a one-way door.

flowchart LR
  A["0"] --> B["1"] --> C["2"] --> D["3"] --> E["4"] --> F["5"] --> G["6"]
  G -->|"+1 wraps to 0"| A

The picture above is the mod 7 clock. Counting past 6 lands you back on 0.

The math

Definition

Definition: Congruence modulo n

Let n>0n \gt 0 be an integer, called the modulus. Two integers aa and bb are congruent mod nn, written a≡b(modn)a \equiv b \pmod{n}, if nn divides their difference: n∣(a−b)n \mid (a - b). Equivalently, aa and bb leave the same remainder when divided by nn.

Here aa and bb are ordinary integers, nn is the size of the cycle, and the symbol ∣\mid means "divides", so n∣kn \mid k says that k=n⋅qk = n \cdot q for some integer qq. The remainder of aa after division by nn is written a mod na \bmod n. It always lies between 00 and n−1n-1.

Arithmetic on remainders

Congruence respects the basic operations. If a≡ba \equiv b and c≡dc \equiv d (all mod nn), then

a+c≡b+d,a−c≡b−d,a⋅c≡b⋅d(modn).a + c \equiv b + d, \qquad a - c \equiv b - d, \qquad a \cdot c \equiv b \cdot d \pmod{n}.

So you may reduce at any point. To compute (37⋅46+5) mod 10(37 \cdot 46 + 5) \bmod 10 you can first take each factor mod 10: 37≡737 \equiv 7, 46≡646 \equiv 6, so the expression is ≡7⋅6+5=47≡7\equiv 7 \cdot 6 + 5 = 47 \equiv 7. Keeping numbers small at every step is the practical payoff.

Division does not always work. The integers mod nn form a ring: you can add, subtract and multiply, but you cannot always divide. A number aa has a multiplicative inverse mod nn (a number xx with ax≡1(modn)ax \equiv 1 \pmod{n}) exactly when gcd⁡(a,n)=1\gcd(a, n) = 1, meaning aa and nn share no factor other than 11.

Fermat's little theorem

When the modulus is a prime pp, powers behave very regularly. If pp is prime and pp does not divide aa, then

ap−1≡1(modp).a^{p-1} \equiv 1 \pmod{p}.

This is Fermat's little theorem. For example, with p=7p = 7 and a=3a = 3, the earlier powers 34≡43^4 \equiv 4, 35≡12≡53^5 \equiv 12 \equiv 5 and 36≡15≡1(mod7)3^6 \equiv 15 \equiv 1 \pmod 7 confirm it. The theorem underlies certain primality tests and the correctness of RSA decryption.

Worked examples

Example
Hand-computable: the clock

Compute 1515 hours after 99 o'clock on a 12-hour clock.

  1. Add the hours: 9+15=249 + 15 = 24.
  2. Reduce mod 12: 24 mod 12=024 \bmod 12 = 0, since 24=2⋅12+024 = 2 \cdot 12 + 0.
  3. A remainder of 00 on a 12-hour clock is shown as 1212.

Answer: 1212 o'clock. Interpretation: 15 hours is one full lap plus 3 hours, and 9 plus 3 is 12.

Example
Negative numbers and remainders

Find −17 mod 5-17 \bmod 5. Many calculators return a negative remainder here, so be careful.

  1. Pick the multiple of 5 just below −17-17: that is −20=−4⋅5-20 = -4 \cdot 5.
  2. Subtract: −17−(−20)=3-17 - (-20) = 3.
  3. Since 0≤3<50 \le 3 \lt 5, the remainder is 33.

Answer: −17≡3(mod5)-17 \equiv 3 \pmod 5. Interpretation: going 17 steps backward on a 5-cycle lands on the same spot as going 3 steps forward.

Example
Realistic: what day will it be?

Today is Monday. What day is it 100 days from now? A week has 7 days, so only the remainder of 100 matters.

  1. Compute 100 mod 7100 \bmod 7. Since 7⋅14=987 \cdot 14 = 98, the remainder is 100−98=2100 - 98 = 2.
  2. Monday plus 2 days is Wednesday.

Answer: Wednesday. Interpretation: 98 days is exactly 14 weeks, so only the extra 2 days move the weekday.

Example
Powers mod 7 by repeated multiplication

Compute 34 mod 73^4 \bmod 7.

  1. 31=33^1 = 3.
  2. 32=93^2 = 9, and 9 mod 7=29 \bmod 7 = 2.
  3. 33≡3⋅2=6(mod7)3^3 \equiv 3 \cdot 2 = 6 \pmod 7.
  4. 34≡3⋅6=18≡4(mod7)3^4 \equiv 3 \cdot 6 = 18 \equiv 4 \pmod 7, since 18−14=418 - 14 = 4.

Check: 34=813^4 = 81, and 81−77=481 - 77 = 4. Answer: 44. Interpretation: reducing at each step gives the same answer as computing the big number first, with much smaller intermediate values.

Example
A check digit with mod 9

The digit sum of a number leaves the same remainder mod 9 as the number itself. Take 12341234.

  1. Digit sum: 1+2+3+4=101 + 2 + 3 + 4 = 10.
  2. Digit sum of 10: 1+0=11 + 0 = 1.
  3. Check: 1234=137⋅9+11234 = 137 \cdot 9 + 1, and 137⋅9=1233137 \cdot 9 = 1233.

Answer: 1234≡1(mod9)1234 \equiv 1 \pmod 9. Interpretation: if a copied number has a different digit sum mod 9, a copying error happened. Accountants used this check by hand for centuries.

Tradeoffs and failure modes

Modular arithmetic is a tool for problems that repeat. It is the wrong tool when you need the actual size of a quantity, because the remainder throws that information away.

SituationWhat goes wrongBetter choice
You need the true value of a quantityRemainders lose the size, so 1717 and 22 look the same mod 5Keep ordinary integers, or use a big-integer library
Dividing by a number that shares a factor with the modulusThere is no inverse, so the answer is undefined or wrongCheck gcd⁡(a,n)=1\gcd(a, n) = 1 first, or work mod a prime
Negative results from a language's % operatorSome languages return a negative remainderNormalise with ((a % n) + n) % n
Overflow in fixed-width integersIntermediate products exceed the type's maximumReduce after each multiplication
Tiny moduli with long runsPatterns repeat quickly, so brute force finds themUse the structure of the cycle instead

History

Modular reasoning is old. The Chinese text Sunzi Suanjing, usually dated to somewhere between the 3rd and 5th centuries, contains a method for solving systems of congruences, now called the Chinese remainder theorem. The clock picture and the notation came much later.

Carl Friedrich Gauss introduced the symbol ≡\equiv and the systematic theory in his Disquisitiones Arithmeticae, published in 1801. He was working on questions about prime numbers and quadratic equations, and the notation made patterns in those questions easy to see. The theory caught on because it turned messy questions about integers into questions about cycles, which mathematicians could handle with the new tools of algebra.

Its biggest practical jump came in the 1970s. Modular exponentiation, computing ak mod na^k \bmod n quickly even for huge numbers, underlies the Diffie-Hellman key exchange (1976) and the RSA cryptosystem (1977). Those schemes rely on the fact that the forward computation is fast while reversing it is believed to be hard.

Check yourself

QuestionWhat is the remainder of 47 mod 947 \bmod 9, and why is that the right answer?Show answer

47=5⋅9+247 = 5 \cdot 9 + 2, so the remainder is 22. The remainder is the leftover after removing as many full cycles of length 99 as possible, and it must lie between 00 and 88.

QuestionCompute (23⋅19) mod 10(23 \cdot 19) \bmod 10 by reducing first.Show answer

Reduce each factor: 23≡323 \equiv 3 and 19≡9(mod10)19 \equiv 9 \pmod{10}. Then 3⋅9=27≡7(mod10)3 \cdot 9 = 27 \equiv 7 \pmod{10}. Computing the full product first gives 437437, and 437 mod 10=7437 \bmod 10 = 7, which agrees. Reducing first is the same answer with smaller numbers.

QuestionWhy does 22 have no multiplicative inverse mod 66?Show answer

An inverse xx would satisfy 2x≡1(mod6)2x \equiv 1 \pmod 6, which means 66 divides 2x−12x - 1. But 2x−12x - 1 is odd and 66 is even, so no such xx exists. The reason is that gcd⁡(2,6)=2≠1\gcd(2, 6) = 2 \ne 1, which is the condition for an inverse to exist.

QuestionA 12-hour clock shows 10 o'clock. What time is it 50 hours later, and how do you get it?Show answer

Reduce 5050 mod 1212: 50=4⋅12+250 = 4 \cdot 12 + 2, so the remainder is 22. Then 10+2=1210 + 2 = 12, and the clock shows 12 o'clock. The four full laps of 12 hours bring the hand back to where it started, so only the leftover 2 hours matter.

QuestionYou are building a loop that cycles through 7 slots. How would you advance from slot 6 to the next slot in code, and why is the modulus needed?Show answer

Use next = (current + 1) % 7. Without the modulus, the index would go to 77, which is not a valid slot. The % 7 wraps 77 back to 00, so the index always stays in 00 to 66.

Sources