Modular Arithmetic
Learning objectives
- Define congruence modulo and verify
- Add, subtract, multiply, and exponentiate inside
- Solve linear congruences
- Recognise when a modular inverse exists
Modular arithmetic is what happens when numbers live on a clock face instead of a number line. Add two hours past and you do not get — you get . That single twist, “wrap around at ,” is the foundation of every hash table key, every check-digit on an ISBN or credit card, every cyclic redundancy check (CRC) error-detection scheme inside Ethernet frames, every cryptographic key exchange. The remainder operation is fast, deterministic, and folds an unbounded space of integers into a finite set of equivalence classes — which is exactly the data structure you want for indexing, signing, and verifying.
The definition: congruence
For a positive integer , we say is congruent to modulo , written , when . Equivalently, and leave the same remainder when divided by . Example: because .
Congruence is an equivalence relation: reflexive (), symmetric (), and transitive ( and give ). Equivalence relations carve the integers into classes; the classes for modulus are written and collectively form the set .
Arithmetic respects congruence
If and , then , , , and for any . This is what makes a ring: addition and multiplication are well-defined on the equivalence classes. Division, however, is more delicate — we will get there.
(Set the modulus in the widget above and type a single integer; the hand sweeps to its representative class. Try increasing values of past the modulus and observe how the hand wraps around. To see addition wrap, compute and on paper, then enter the sums into the widget to confirm.)
Modular inverses and linear congruences
The linear congruence asks for an such that is a multiple of . It has a solution if and only if . The cleanest special case: . In that case has a unique modular inverse mod , and the solution is . The inverse comes from Bezout: write ; then , so .
Worked example. Solve . Since , an inverse exists. Try small multiples: , so . Then . Check: .
Pause and think: What is for any ? Now — harder — what is ? The fact that congruence respects exponentiation is doing real work for you in both answers.
Try it
- Predict: what is ? (Hint: .) Verify by reducing first, then exponentiating.
- Solve . Find by trial, then multiply.
- Compute without ever computing directly. (Hint: ; how does that help?)
- Determine for which the element has a modular inverse. Justify each case using .
A trap to watch for
Addition, subtraction, and multiplication all carry over from to without surprises. Division does not. You cannot blindly cancel: but if you divide both sides by , you get , which is false. The right statement is: if , then implies . Cancellation is safe exactly when the cancelled factor is coprime to the modulus. A related trap: students sometimes write — but is the remainder, not the quotient.
What you now know
You can compute inside , find modular inverses when they exist, and solve linear congruences. The next section, Euler’s theorem, generalises Fermat’s little theorem () to any modulus — replacing “” with , the count of units in .
References
- Velleman, D. J. (2019). How to Prove It: A Structured Approach (3rd ed.). Cambridge University Press, §7.3.
- Hardy, G. H., Wright, E. M. (2008). An Introduction to the Theory of Numbers (6th ed.). Oxford University Press, ch. 5.
- Niven, I., Zuckerman, H. S., Montgomery, H. L. (1991). An Introduction to the Theory of Numbers (5th ed.). Wiley, ch. 2.
- Rosen, K. H. (2010). Elementary Number Theory (6th ed.). Pearson, ch. 4.
- Stinson, D. R., Paterson, M. B. (2018). Cryptography: Theory and Practice (4th ed.). CRC Press, ch. 5.