Skip to content

Modular Arithmetic

In this lesson you’ll learn the division algorithm, what congruence modulo nn means, how to add and multiply in modular arithmetic, why division is the tricky operation, and how modular exponentiation makes modern cryptography possible.

For any integer aa and positive integer nn, there exist unique integers qq (quotient) and rr (remainder) with

a=qn+r0r<na = qn + r \qquad 0 \leq r < n

We write r=amodnr = a \bmod n.

17=35+217mod5=217 = 3 \cdot 5 + 2 \quad \Longrightarrow \quad 17 \bmod 5 = 2

The remainder is always in {0,1,,n1}\{0, 1, \ldots, n-1\}. That constraint is what makes it well-defined.

Negative numbers. In mathematics the remainder is always non-negative:

7=(2)(5)+37mod5=3-7 = (-2)(5) + 3 \quad \Longrightarrow \quad -7 \bmod 5 = 3

Be aware that many programming languages disagree. In C, Java, and JavaScript, -7 % 5 returns 3-3, 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.

ab(modn)meansn(ab)a \equiv b \pmod{n} \quad \text{means} \quad n \mid (a - b)

Read: ”aa is congruent to bb modulo nn.” Equivalently, aa and bb leave the same remainder when divided by nn.

172(mod5)383(mod5)73(mod5)17 \equiv 2 \pmod{5} \qquad 38 \equiv 3 \pmod{5} \qquad -7 \equiv 3 \pmod{5}

From the relations lesson: congruence mod nn is an equivalence relation, and it partitions the integers into nn 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 153(mod12)15 \equiv 3 \pmod{12}. Days of the week are mod 7. Angles are mod 360.

Congruence plays nicely with addition, subtraction, and multiplication. 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} akbk(modn)a^k \equiv b^k \pmod{n}

This is enormously practical. You can reduce mod nn at any point in a calculation, which keeps numbers small.

To compute 47×63mod1047 \times 63 \bmod 10, don’t multiply first. Reduce first:

47×637×3=211(mod10)47 \times 63 \equiv 7 \times 3 = 21 \equiv 1 \pmod{10}

Check: 47×63=296147 \times 63 = 2961, which ends in 1. Correct.

You cannot freely divide in modular arithmetic. From 612(mod6)6 \equiv 12 \pmod 6 you cannot cancel a 3 to get 24(mod6)2 \equiv 4 \pmod 6, which is false.

Instead of dividing, you multiply by a modular inverse. The inverse of aa mod nn is a value a1a^{-1} with

aa11(modn)a \cdot a^{-1} \equiv 1 \pmod{n}

It exists if and only if gcd(a,n)=1\gcd(a, n) = 1, meaning aa and nn are coprime.

For example, mod 7: 35=151(mod7)3 \cdot 5 = 15 \equiv 1 \pmod 7, so 315(mod7)3^{-1} \equiv 5 \pmod 7.

But mod 6, the value 2 has no inverse, because gcd(2,6)=2\gcd(2,6) = 2. 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 pp is prime, every nonzero value from 1 to p1p-1 is coprime to pp, 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.

Computing akmodna^k \bmod n is the core operation of public-key cryptography, and the naive approach is hopeless. 71007^{100} 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 kk multiplications, use about log2k\log_2 k of them by repeated squaring.

The squaring trick looks like this:

a16=(((a2)2)2)2a^{16} = \big(\big(\big(a^2\big)^2\big)^2\big)^2

Four squarings instead of fifteen multiplications. For a 2048-bit exponent this is the difference between “instant” and “longer than the universe has existed.”

If pp is prime and pap \nmid a (read: ”pp does not divide aa,” so aa is not a multiple of pp), then

ap11(modp)a^{p-1} \equiv 1 \pmod{p}

That side condition is doing real work. If aa were a multiple of pp, then a0a \equiv 0, and 0p1=00^{p-1} = 0 rather than 1. Try p=5p = 5: for a=2a = 2 you get 24=161(mod5)2^4 = 16 \equiv 1 \pmod 5, but for a=10a = 10 you get 1040(mod5)10^4 \equiv 0 \pmod 5. Every aa that isn’t a multiple of 5 lands on 1.

This collapses huge exponents. To compute 3100mod73^{100} \bmod 7, note 361(mod7)3^6 \equiv 1 \pmod 7, so reduce the exponent mod 6:

100=166+4310034=814(mod7)100 = 16 \cdot 6 + 4 \quad \Longrightarrow \quad 3^{100} \equiv 3^4 = 81 \equiv 4 \pmod 7

The theorem also underlies primality testing. If an1≢1(modn)a^{n-1} \not\equiv 1 \pmod n for some aa, then nn is definitely composite, and you learned that without factoring it.

Example 1: Compute remainders.

Find amod7a \bmod 7 for a=45a = 45, a=7a = 7, a=12a = -12.

Solution.

45=67+345 = 6 \cdot 7 + 3, so 45mod7=345 \bmod 7 = 3.

7=17+07 = 1 \cdot 7 + 0, so 7mod7=07 \bmod 7 = 0.

12=(2)(7)+2-12 = (-2)(7) + 2, so 12mod7=2-12 \bmod 7 = 2. (Go down to the next multiple of 7 below 12-12, which is 14-14, then the remainder is the distance up: 12(14)=2-12 - (-14) = 2.)

Example 2: Reduce as you go.

Compute (83+129)×47mod9(83 + 129) \times 47 \bmod 9.

Solution. Reduce each piece first.

83mod983 \bmod 9: 99=819 \cdot 9 = 81, so 83283 \equiv 2.

129mod9129 \bmod 9: 914=1269 \cdot 14 = 126, so 1293129 \equiv 3.

47mod947 \bmod 9: 95=459 \cdot 5 = 45, so 47247 \equiv 2.

(2+3)×2=101(mod9)(2 + 3) \times 2 = 10 \equiv 1 \pmod 9

The answer is 1. Direct computation gives 212×47=9964212 \times 47 = 9964, and 9964=91107+19964 = 9 \cdot 1107 + 1. Confirmed, with far less arithmetic.

Example 3: A large power.

Compute 320mod113^{20} \bmod 11.

Solution. Use repeated squaring, reducing every time.

32=93^2 = 9 34=92=814(mod11)3^4 = 9^2 = 81 \equiv 4 \pmod{11} 3842=165(mod11)3^8 \equiv 4^2 = 16 \equiv 5 \pmod{11} 31652=253(mod11)3^{16} \equiv 5^2 = 25 \equiv 3 \pmod{11}

Now 20=16+420 = 16 + 4:

320=3163434=121(mod11)3^{20} = 3^{16} \cdot 3^4 \equiv 3 \cdot 4 = 12 \equiv 1 \pmod{11}

The answer is 1. Note that 3203^{20} is about 3.5 billion, and we never computed a number bigger than 25.

Faster route with Fermat: 11 is prime, so 3101(mod11)3^{10} \equiv 1 \pmod{11}, and 320=(310)213^{20} = (3^{10})^2 \equiv 1.

Example 4: Find a modular inverse.

Find 51mod125^{-1} \bmod 12.

Solution. First check it exists: gcd(5,12)=1\gcd(5,12) = 1, so yes.

For small moduli, just test values until 5k15k \equiv 1:

  • 51=55 \cdot 1 = 5
  • 52=105 \cdot 2 = 10
  • 53=1535 \cdot 3 = 15 \equiv 3
  • 54=2085 \cdot 4 = 20 \equiv 8
  • 55=2515 \cdot 5 = 25 \equiv 1

So 515(mod12)5^{-1} \equiv 5 \pmod{12}. (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 4x6(mod10)4x \equiv 6 \pmod{10}.

Solution. Careful, gcd(4,10)=2\gcd(4,10) = 2, so 4 has no inverse mod 10 and we can’t just multiply through.

Test all residues x=0x = 0 through 9:

  • x=4x=4: 16616 \equiv 6
  • x=9x=9: 36636 \equiv 6

All other values fail. So x4x \equiv 4 or x9(mod10)x \equiv 9 \pmod{10}, giving two solution classes.

The general rule: axb(modn)ax \equiv b \pmod n has solutions exactly when gcd(a,n)b\gcd(a,n) \mid b, and then it has exactly gcd(a,n)\gcd(a,n) solutions mod nn. Here gcd=2\gcd = 2 and 262 \mid 6, 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.

500=717+35003(mod7)500 = 71 \cdot 7 + 3 \quad \Longrightarrow \quad 500 \equiv 3 \pmod 7

Three days after Wednesday is Saturday.

Example 7: A check digit.

ISBN-10 check digits satisfy

i=110idi0(mod11)\sum_{i=1}^{10} i \cdot d_i \equiv 0 \pmod{11}

where did_i is the ii-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.

1(0)+2(3)+3(0)+4(6)+5(4)+6(0)+7(6)+8(1)+9(5)+10(2)1(0) + 2(3) + 3(0) + 4(6) + 5(4) + 6(0) + 7(6) + 8(1) + 9(5) + 10(2) =0+6+0+24+20+0+42+8+45+20=165= 0 + 6 + 0 + 24 + 20 + 0 + 42 + 8 + 45 + 20 = 165 165=15111650(mod11)165 = 15 \cdot 11 \quad \Longrightarrow \quad 165 \equiv 0 \pmod{11}

Valid. The weighting by position is what lets this scheme catch transposition errors, not just single-digit typos.

Public-key cryptography is modular arithmetic. RSA encrypts with cme(modn)c \equiv m^e \pmod n and decrypts with mcd(modn)m \equiv c^d \pmod n, where nn is a product of two large primes. Its security rests on the fact that modular exponentiation is fast while factoring nn to recover dd 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 xn+1=(axn+c)modmx_{n+1} = (ax_n + c) \bmod m. Their quality depends entirely on the choice of aa, cc, and mm.

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.

What is 43 mod 8?
The statement 'a is congruent to b modulo n' means...
Compute (37 x 54) mod 6 by reducing first.
When does a value a have a multiplicative inverse modulo n?
Why is modular exponentiation computed by repeated squaring with reduction at each step?