Modular Arithmetic

Chapter 7: Number Theory

Learning objectives

  • Define congruence modulo nn and verify ab(modn)a \equiv b \pmod{n}
  • Add, subtract, multiply, and exponentiate inside Z/nZ\mathbb{Z}/n\mathbb{Z}
  • Solve linear congruences axb(modn)ax \equiv b \pmod{n}
  • 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 1111 and you do not get 1313 — you get 11. That single twist, “wrap around at nn,” 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 amodna \bmod n is fast, deterministic, and folds an unbounded space of integers into a finite set of nn equivalence classes — which is exactly the data structure you want for indexing, signing, and verifying.

The definition: congruence

For a positive integer nn, we say aa is congruent to bb modulo nn, written ab(modn)a \equiv b \pmod{n}, when n(ab)n \mid (a - b). Equivalently, aa and bb leave the same remainder when divided by nn. Example: 172(mod5)17 \equiv 2 \pmod{5} because 5(172)=155 \mid (17 - 2) = 15.

Congruence is an equivalence relation: reflexive (aaa \equiv a), symmetric (abbaa \equiv b \Rightarrow b \equiv a), and transitive (aba \equiv b and bcb \equiv c give aca \equiv c). Equivalence relations carve the integers into classes; the nn classes for modulus nn are written [0],[1],,[n1][0], [1], \ldots, [n-1] and collectively form the set Z/nZ\mathbb{Z}/n\mathbb{Z}.

Arithmetic respects congruence

If ab(modn)a \equiv b \pmod{n} and cd(modn)c \equiv d \pmod{n}, then a+cb+d(modn)a + c \equiv b + d \pmod{n}, acbd(modn)a - c \equiv b - d \pmod{n}, acbd(modn)ac \equiv bd \pmod{n}, and akbk(modn)a^k \equiv b^k \pmod{n} for any k1k \geq 1. This is what makes Z/nZ\mathbb{Z}/n\mathbb{Z} a ring: addition and multiplication are well-defined on the equivalence classes. Division, however, is more delicate — we will get there.

Mod ClockInteractive figure — enable JavaScript to interact.

(Set the modulus in the widget above and type a single integer; the hand sweeps to its representative class. Try increasing values of nn past the modulus and observe how the hand wraps around. To see addition wrap, compute (7+5)mod12(7 + 5) \bmod 12 and (10+6)mod12(10 + 6) \bmod 12 on paper, then enter the sums into the widget to confirm.)

Modular inverses and linear congruences

The linear congruence axb(modn)ax \equiv b \pmod{n} asks for an xx such that axbax - b is a multiple of nn. It has a solution if and only if gcd(a,n)b\gcd(a, n) \mid b. The cleanest special case: gcd(a,n)=1\gcd(a, n) = 1. In that case aa has a unique modular inverse a1a^{-1} mod nn, and the solution is xa1b(modn)x \equiv a^{-1} b \pmod{n}. The inverse comes from Bezout: write sa+tn=1sa + tn = 1; then sa1(modn)sa \equiv 1 \pmod{n}, so a1s(modn)a^{-1} \equiv s \pmod{n}.

Worked example. Solve 3x5(mod7)3x \equiv 5 \pmod{7}. Since gcd(3,7)=1\gcd(3, 7) = 1, an inverse exists. Try small multiples: 35=151(mod7)3 \cdot 5 = 15 \equiv 1 \pmod{7}, so 315(mod7)3^{-1} \equiv 5 \pmod{7}. Then x55=254(mod7)x \equiv 5 \cdot 5 = 25 \equiv 4 \pmod{7}. Check: 34=125(mod7)3 \cdot 4 = 12 \equiv 5 \pmod{7}.

Pause and think: What is (1)100(modn)(-1)^{100} \pmod{n} for any n2n \geq 2? Now — harder — what is (1)101(modn)(-1)^{101} \pmod{n}? The fact that congruence respects exponentiation is doing real work for you in both answers.

Try it

  • Predict: what is 7100mod47^{100} \bmod 4? (Hint: 731(mod4)7 \equiv 3 \equiv -1 \pmod{4}.) Verify by reducing 77 first, then exponentiating.
  • Solve 5x3(mod11)5x \equiv 3 \pmod{11}. Find 51mod115^{-1} \bmod 11 by trial, then multiply.
  • Compute 210mod72^{10} \bmod 7 without ever computing 210=10242^{10} = 1024 directly. (Hint: 23=812^3 = 8 \equiv 1; how does that help?)
  • Determine for which n{2,3,4,5,6}n \in {2, 3, 4, 5, 6} the element 44 has a modular inverse. Justify each case using gcd(4,n)\gcd(4, n).

A trap to watch for

Addition, subtraction, and multiplication all carry over from Z\mathbb{Z} to Z/nZ\mathbb{Z}/n\mathbb{Z} without surprises. Division does not. You cannot blindly cancel: 60(mod6)6 \equiv 0 \pmod{6} but if you divide both sides by 22, you get 30(mod6)3 \equiv 0 \pmod{6}, which is false. The right statement is: if gcd(c,n)=1\gcd(c, n) = 1, then cacb(modn)ca \equiv cb \pmod{n} implies ab(modn)a \equiv b \pmod{n}. Cancellation is safe exactly when the cancelled factor is coprime to the modulus. A related trap: students sometimes write amodn=a/na \bmod n = a/n — but amodna \bmod n is the remainder, not the quotient.

Code ChallengeInteractive figure — enable JavaScript to interact.

What you now know

You can compute inside Z/nZ\mathbb{Z}/n\mathbb{Z}, find modular inverses when they exist, and solve linear congruences. The next section, Euler’s theorem, generalises Fermat’s little theorem (ap11(modp)a^{p-1} \equiv 1 \pmod{p}) to any modulus — replacing “p1p - 1” with ϕ(n)\phi(n), the count of units in Z/nZ\mathbb{Z}/n\mathbb{Z}.

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.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.