Modular Arithmetic
What You’ll Learn
Section titled “What You’ll Learn”In this lesson you’ll learn the division algorithm, what congruence modulo means, how to add and multiply in modular arithmetic, why division is the tricky operation, and how modular exponentiation makes modern cryptography possible.
The Concept
Section titled “The Concept”The division algorithm
Section titled “The division algorithm”For any integer and positive integer , there exist unique integers (quotient) and (remainder) with
We write .
The remainder is always in . That constraint is what makes it well-defined.
Negative numbers. In mathematics the remainder is always non-negative:
Be aware that many programming languages disagree. In C, Java, and JavaScript, -7 % 5 returns , because they truncate the quotient toward zero. Python returns 3, matching the mathematical convention. This is a real source of bugs, so check your language.
Congruence
Section titled “Congruence”Read: ” is congruent to modulo .” Equivalently, and leave the same remainder when divided by .
From the relations lesson: congruence mod is an equivalence relation, and it partitions the integers into residue classes. Modular arithmetic is arithmetic on those classes.
The intuition everyone already has is a clock. On a 12-hour clock, 15:00 is 3 o’clock because . Days of the week are mod 7. Angles are mod 360.
Arithmetic that works
Section titled “Arithmetic that works”Congruence plays nicely with addition, subtraction, and multiplication. If and , then
This is enormously practical. You can reduce mod at any point in a calculation, which keeps numbers small.
To compute , don’t multiply first. Reduce first:
Check: , which ends in 1. Correct.
Division does not work the same way
Section titled “Division does not work the same way”You cannot freely divide in modular arithmetic. From you cannot cancel a 3 to get , which is false.
Instead of dividing, you multiply by a modular inverse. The inverse of mod is a value with
It exists if and only if , meaning and are coprime.
For example, mod 7: , so .
But mod 6, the value 2 has no inverse, because . No multiple of 2 is ever congruent to 1 mod 6, since all multiples of 2 are even and 1 mod 6 lands on odd numbers.
This is why prime moduli are special. If is prime, every nonzero value from 1 to is coprime to , so every one of them has an inverse. That makes arithmetic mod a prime behave almost exactly like ordinary arithmetic, which is why cryptography uses primes constantly.
Modular exponentiation
Section titled “Modular exponentiation”Computing is the core operation of public-key cryptography, and the naive approach is hopeless. has 85 digits, and real cryptography uses exponents in the thousands of bits.
Two ideas fix it.
- Reduce at every step - never let the number grow past the modulus.
- Square and multiply - instead of multiplications, use about of them by repeated squaring.
The squaring trick looks like this:
Four squarings instead of fifteen multiplications. For a 2048-bit exponent this is the difference between “instant” and “longer than the universe has existed.”
Fermat’s little theorem
Section titled “Fermat’s little theorem”If is prime and (read: ” does not divide ,” so is not a multiple of ), then
That side condition is doing real work. If were a multiple of , then , and rather than 1. Try : for you get , but for you get . Every that isn’t a multiple of 5 lands on 1.
This collapses huge exponents. To compute , note , so reduce the exponent mod 6:
The theorem also underlies primality testing. If for some , then is definitely composite, and you learned that without factoring it.
Worked Examples
Section titled “Worked Examples”Example 1: Compute remainders.
Find for , , .
Solution.
, so .
, so .
, so . (Go down to the next multiple of 7 below , which is , then the remainder is the distance up: .)
Example 2: Reduce as you go.
Compute .
Solution. Reduce each piece first.
: , so .
: , so .
: , so .
The answer is 1. Direct computation gives , and . Confirmed, with far less arithmetic.
Example 3: A large power.
Compute .
Solution. Use repeated squaring, reducing every time.
Now :
The answer is 1. Note that is about 3.5 billion, and we never computed a number bigger than 25.
Faster route with Fermat: 11 is prime, so , and .
Example 4: Find a modular inverse.
Find .
Solution. First check it exists: , so yes.
For small moduli, just test values until :
- ✓
So . (5 is its own inverse here, which happens sometimes.)
For large moduli you’d use the extended Euclidean algorithm rather than guessing.
Example 5: Solve a congruence.
Solve .
Solution. Careful, , so 4 has no inverse mod 10 and we can’t just multiply through.
Test all residues through 9:
- : ✓
- : ✓
All other values fail. So or , giving two solution classes.
The general rule: has solutions exactly when , and then it has exactly solutions mod . Here and , so two solutions, as found.
Example 6: Day of the week.
Today is Wednesday. What day is it in 500 days?
Solution. Days cycle mod 7.
Three days after Wednesday is Saturday.
Example 7: A check digit.
ISBN-10 check digits satisfy
where is the -th digit read left to right. Verify that 0-306-40615-2 is valid.
Solution. Digits: 0, 3, 0, 6, 4, 0, 6, 1, 5, 2.
Valid. The weighting by position is what lets this scheme catch transposition errors, not just single-digit typos.
Real-World Applications
Section titled “Real-World Applications”Public-key cryptography is modular arithmetic. RSA encrypts with and decrypts with , where is a product of two large primes. Its security rests on the fact that modular exponentiation is fast while factoring to recover is not. Diffie-Hellman key exchange, which secures most HTTPS connections, relies on the difficulty of the discrete logarithm problem in modular arithmetic.
Hash tables use hash(key) % table_size to map keys into buckets. Choosing a prime table size is a modular arithmetic decision: it spreads keys more evenly when the hash values happen to share factors with the table size.
Check digits are everywhere. Credit card numbers use the Luhn algorithm mod 10, ISBNs use mod 11 or mod 10, and bank routing numbers, VINs, and shipping barcodes all use similar schemes to catch typos before they cause problems.
Cyclic buffers and ring data structures wrap with (index + 1) % capacity, which is how audio buffers, network packet queues, and streaming windows manage fixed memory without reallocating.
Pseudorandom number generators are built on modular recurrences of the form . Their quality depends entirely on the choice of , , and .
And calendars are modular arithmetic in the wild. Leap year rules, the day-of-week calculation for any date in history, and the calculation of Easter are all congruence problems.
Retrying will remove your ✅ checkmark until you pass again.