TL;DR
Modular arithmetic is counting that wraps around. You pick a size , and after you reach you start again from , the way a clock goes from 12 back to 1. Two whole numbers are congruent mod when they leave the same remainder after division by , 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 "", read "14 is congruent to 2 mod 12". The symbol means "same position on the clock". The number 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
Let be an integer, called the modulus. Two integers and are congruent mod , written , if divides their difference: . Equivalently, and leave the same remainder when divided by .
Here and are ordinary integers, is the size of the cycle, and the symbol means "divides", so says that for some integer . The remainder of after division by is written . It always lies between and .
Arithmetic on remainders
Congruence respects the basic operations. If and (all mod ), then
So you may reduce at any point. To compute you can first take each factor mod 10: , , so the expression is . Keeping numbers small at every step is the practical payoff.
Division does not always work. The integers mod form a ring: you can add, subtract and multiply, but you cannot always divide. A number has a multiplicative inverse mod (a number with ) exactly when , meaning and share no factor other than .
Fermat's little theorem
When the modulus is a prime , powers behave very regularly. If is prime and does not divide , then
This is Fermat's little theorem. For example, with and , the earlier powers , and confirm it. The theorem underlies certain primality tests and the correctness of RSA decryption.
Worked examples
Compute hours after o'clock on a 12-hour clock.
- Add the hours: .
- Reduce mod 12: , since .
- A remainder of on a 12-hour clock is shown as .
Answer: o'clock. Interpretation: 15 hours is one full lap plus 3 hours, and 9 plus 3 is 12.
Find . Many calculators return a negative remainder here, so be careful.
- Pick the multiple of 5 just below : that is .
- Subtract: .
- Since , the remainder is .
Answer: . Interpretation: going 17 steps backward on a 5-cycle lands on the same spot as going 3 steps forward.
Today is Monday. What day is it 100 days from now? A week has 7 days, so only the remainder of 100 matters.
- Compute . Since , the remainder is .
- Monday plus 2 days is Wednesday.
Answer: Wednesday. Interpretation: 98 days is exactly 14 weeks, so only the extra 2 days move the weekday.
Compute .
- .
- , and .
- .
- , since .
Check: , and . Answer: . Interpretation: reducing at each step gives the same answer as computing the big number first, with much smaller intermediate values.
The digit sum of a number leaves the same remainder mod 9 as the number itself. Take .
- Digit sum: .
- Digit sum of 10: .
- Check: , and .
Answer: . 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.
| Situation | What goes wrong | Better choice |
|---|---|---|
| You need the true value of a quantity | Remainders lose the size, so and look the same mod 5 | Keep ordinary integers, or use a big-integer library |
| Dividing by a number that shares a factor with the modulus | There is no inverse, so the answer is undefined or wrong | Check first, or work mod a prime |
Negative results from a language's % operator | Some languages return a negative remainder | Normalise with ((a % n) + n) % n |
| Overflow in fixed-width integers | Intermediate products exceed the type's maximum | Reduce after each multiplication |
| Tiny moduli with long runs | Patterns repeat quickly, so brute force finds them | Use 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 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 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 , and why is that the right answer?Show answer
, so the remainder is . The remainder is the leftover after removing as many full cycles of length as possible, and it must lie between and .
QuestionCompute by reducing first.Show answer
Reduce each factor: and . Then . Computing the full product first gives , and , which agrees. Reducing first is the same answer with smaller numbers.
QuestionWhy does have no multiplicative inverse mod ?Show answer
An inverse would satisfy , which means divides . But is odd and is even, so no such exists. The reason is that , 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 mod : , so the remainder is . Then , 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 , which is not a valid slot. The % 7 wraps back to , so the index always stays in to .