Add, subtract, multiply, and exponentiate inside Z/nZ
Solve linear congruences axβ‘b(modn)
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 11 and you do not get 13, you get 1. That single twist, βwrap around at n,β 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 amodn is fast, deterministic, and folds an unbounded space of integers into a finite set of n equivalence classes, which is exactly the data structure you want for indexing, signing, and verifying.
The definition: congruence
For a positive integer n, we say a is congruent to b modulo n, written aβ‘b(modn), when nβ£(aβb). Equivalently, a and b leave the same remainder when divided by n. Example: 17β‘2(mod5) because 5β£(17β2)=15.
Congruence is an equivalence relation: reflexive (aβ‘a), symmetric (aβ‘bβbβ‘a), and transitive (aβ‘b and bβ‘c give aβ‘c). Equivalence relations carve the integers into classes; the n classes for modulus n are written [0],[1],β¦,[nβ1] and collectively form the set Z/nZ.
Arithmetic respects congruence
If aβ‘b(modn) and cβ‘d(modn), then a+cβ‘b+d(modn), aβcβ‘bβd(modn), acβ‘bd(modn), and akβ‘bk(modn) for any kβ₯1. This is what makes Z/nZ 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 n past the modulus and observe how the hand wraps around. To see addition wrap, compute (7+5)mod12 and (10+6)mod12 on paper, then enter the sums into the widget to confirm.)
Modular inverses and linear congruences
The linear congruence axβ‘b(modn) asks for an x such that axβb is a multiple of n. It has a solution if and only if gcd(a,n)β£b. The cleanest special case: gcd(a,n)=1. In that case a has a unique modular inverse aβ1 mod n, and the solution is xβ‘aβ1b(modn). The inverse comes from Bezout: write sa+tn=1; then saβ‘1(modn), so aβ1β‘s(modn).
Worked example. Solve 3xβ‘5(mod7). Since gcd(3,7)=1, an inverse exists. Try small multiples: 3β 5=15β‘1(mod7), so 3β1β‘5(mod7). Then xβ‘5β 5=25β‘4(mod7). Check: 3β 4=12β‘5(mod7).
Pause and think: What is (β1)100(modn) for any nβ₯2? Now, harder, what is (β1)101(modn)? The fact that congruence respects exponentiation is doing real work for you in both answers.
Try it
Predict: what is 7100mod4? (Hint: 7β‘3β‘β1(mod4).) Verify by reducing 7 first, then exponentiating.
Solve 5xβ‘3(mod11). Find 5β1mod11 by trial, then multiply.
Compute 210mod7 without ever computing 210=1024 directly. (Hint: 23=8β‘1; how does that help?)
Determine for which nβ{2,3,4,5,6} the element 4 has a modular inverse. Justify each case using gcd(4,n).
A trap to watch for
Addition, subtraction, and multiplication all carry over from Z to Z/nZ without surprises. Division does not. You cannot blindly cancel: 6β‘0(mod6) but if you divide both sides by 2, you get 3β‘0(mod6), which is false. The right statement is: if gcd(c,n)=1, then caβ‘cb(modn) implies aβ‘b(modn). Cancellation is safe exactly when the cancelled factor is coprime to the modulus. A related trap: students sometimes write amodn=a/n, but amodn is the remainder, not the quotient.
What you now know
You can compute inside Z/nZ, find modular inverses when they exist, and solve linear congruences. The next section, Eulerβs theorem, generalises Fermatβs little theorem (apβ1β‘1(modp)) to any modulus, replacing βpβ1β with Ο(n), the count of units in Z/nZ.
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.