Number Theory: Modular Arithmetic

Study Sheet

Number Theory: Modular Arithmetic

Congruences, Fermat, and bases

Working Modulo n

Tip
Congruence arithmetic

a≡b(modn)a\equiv b\pmod n means n∣(a−b)n\mid(a-b). You may add, subtract, and multiply congruences.

In plain terms. Only the remainder matters, so replace big numbers by their remainders and do easy arithmetic.

Example. 3100 mod 53^{100}\bmod 5: since 34=81≡13^4 = 81\equiv 1, 3100=(34)25≡13^{100} = (3^4)^{25}\equiv 1.

Why it works. If a=b+mna = b + mn and c=d+knc = d + kn, then a+ca + c and b+db + d differ by a multiple of nn, and so do ac=bd+n(bk+dm+mkn)ac = bd + n(bk + dm + mkn) and bdbd. So the remainder of a sum or product depends only on the remainders of the pieces. Powers are repeated products, which is why a power's remainder cycles: there are only nn possible remainders, so the sequence a,a2,a3,…a, a^2, a^3, \dots must eventually repeat, and from then on it repeats forever.

Tip
Fermat's little theorem

If pp is prime and p∤ap\nmid a, then ap−1≡1(modp)a^{p-1}\equiv 1\pmod p.

In plain terms. Raising to the power p−1p-1 resets you to 11 modulo a prime, so huge exponents shrink fast.

Example. 2100 mod 72^{100}\bmod 7: 26≡12^6\equiv 1, and 100=6⋅16+4100 = 6\cdot 16 + 4, so 2100≡24=16≡22^{100}\equiv 2^4 = 16\equiv 2.

Example
Worked: a last digit
7931× 7k = 1, 5, 9…k = 2, 6, 10…k = 3, 7, 11…k = 4, 8, 12…
Each step multiplies by 7; after four steps the last digit is back to 1, so exponents only matter mod 4.

What is the last digit of 720277^{2027}?

Spotting it. "Last digit" means "remainder mod 1010", and powers of a fixed number cycle mod 1010. Find the cycle, then reduce the exponent modulo its length.

Step 1. 71≡77^1 \equiv 7, 72=49≡97^2 = 49 \equiv 9, 73≡63≡37^3 \equiv 63 \equiv 3, 74≡21≡17^4 \equiv 21 \equiv 1. The cycle 7,9,3,17, 9, 3, 1 has length 44.

Step 2. 2027=4⋅506+32027 = 4 \cdot 506 + 3, so 72027=(74)506⋅73≡37^{2027} = (7^4)^{506} \cdot 7^3 \equiv 3. The last digit is 33.

Example
Worked: a factorial sum

What is the remainder when 1!+2!+3!+⋯+100!1! + 2! + 3! + \cdots + 100! is divided by 1212?

Spotting it. From some point on, every term is a multiple of 1212 and contributes nothing. Find that point.

Step 1. 4!=244! = 24 is divisible by 1212, and every later factorial is a multiple of 4!4!.

Step 2. Only 1!+2!+3!=91! + 2! + 3! = 9 matters, so the remainder is 99.

Example
Worked: which squares plus one

For how many integers nn with 1≤n≤1001 \le n \le 100 is n2+1n^2 + 1 divisible by 55?

Spotting it. Whether 55 divides n2+1n^2 + 1 depends only on n mod 5n \bmod 5, so check the five residues once and then count.

Step 1. n≡0,1,2,3,4n \equiv 0, 1, 2, 3, 4 give n2+1≡1,2,0,0,2(mod5)n^2 + 1 \equiv 1, 2, 0, 0, 2 \pmod 5. So n≡2n \equiv 2 or 3(mod5)3 \pmod 5.

Step 2. Each block of five consecutive integers contains exactly two such nn, and 11 to 100100 is twenty blocks: 4040 values.

Side note
Where it goes wrong

Reducing the exponent modulo the wrong number. The exponent must be reduced modulo the CYCLE LENGTH, not the modulus. For 2100 mod 72^{100} \bmod 7: reducing 100100 mod 77 gives 22=42^2 = 4, which is wrong. The powers of 22 mod 77 run 2,4,12, 4, 1 with period 33, and 100≡1(mod3)100 \equiv 1 \pmod 3, so 2100≡22^{100} \equiv 2.

Try it
Try it: a power mod 7

What is the remainder when 51005^{100} is divided by 77?

Show the answer

By Fermat, 56≡1(mod7)5^6 \equiv 1 \pmod 7, and 100=6⋅16+4100 = 6 \cdot 16 + 4, so 5100≡54(mod7)5^{100} \equiv 5^4 \pmod 7. Now 52=25≡45^2 = 25 \equiv 4, so 54≡16≡25^4 \equiv 16 \equiv 2. The remainder is 22.

Chinese Remainder and Last-Two-Digit Problems

Tip
Two congruences at once
remainder mod 7012345601234mod 5012345678910111213141516171819202122232425262728293031323334
Each of 0 to 34 lands in its own cell (remainder mod 5, remainder mod 7); 17 is the one in row 2, column 3.

If gcd⁡(m,n)=1\gcd(m,n) = 1, the pair x≡a(modm)x \equiv a \pmod m, x≡b(modn)x \equiv b \pmod n has exactly one solution modulo mnmn.

Solve by substitution: from x≡2(mod5)x \equiv 2 \pmod 5 write x=5t+2x = 5t + 2, substitute into x≡3(mod7)x \equiv 3 \pmod 7 to get 5t≡1(mod7)5t \equiv 1 \pmod 7, so t≡3(mod7)t \equiv 3 \pmod 7 and x≡17(mod35)x \equiv 17 \pmod{35}. Checking both congruences at the end costs seconds and catches the common slip of inverting the wrong coefficient.

Tip
Fermat and Euler for exponents

ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p for a prime p∤ap \nmid a; more generally aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod n when gcd⁡(a,n)=1\gcd(a,n) = 1, with φ(100)=40\varphi(100) = 40.

Last two digits means modulo 100100: 32026=340⋅50+26≡326(mod100)3^{2026} = 3^{40\cdot50 + 26} \equiv 3^{26} \pmod{100}, and squaring up gives 326≡29(mod100)3^{26} \equiv 29 \pmod{100}, so the last two digits are 2929. The trap is applying Euler when gcd⁡(a,n)≠1\gcd(a,n) \ne 1 — for 2k mod 1002^k \bmod 100 the cycle still exists but starts later, so compute 2k2^k modulo 44 and modulo 2525 and recombine.

Example
Worked: a two-congruence puzzle

What is the smallest positive integer leaving remainder 22 when divided by 55 and remainder 33 when divided by 77?

Step 1 — x=5t+2x = 5t + 2.

Step 2 — 5t+2≡3(mod7)5t + 2 \equiv 3 \pmod 7, so 5t≡1(mod7)5t \equiv 1 \pmod 7.

Step 3 — 5−1≡3(mod7)5^{-1} \equiv 3 \pmod 7 (since 15≡115 \equiv 1), so t≡3(mod7)t \equiv 3 \pmod 7 and the smallest is t=3t = 3, x=17x = 17. Check: 17=3⋅5+217 = 3\cdot5 + 2 and 17=2⋅7+317 = 2\cdot7 + 3 ✓

Example
Worked: last two digits by a cycle

What are the last two digits of 57202757^{2027}?

Spotting it. Last two digits means mod 100100. Euler would reduce the exponent mod 4040, but squaring a couple of times shows 5757 cycles much faster: 572=3249≡4957^2 = 3249 \equiv 49, and 492=2401≡149^2 = 2401 \equiv 1.

Step 1. 574≡1(mod100)57^4 \equiv 1 \pmod{100}.

Step 2. 2027=4⋅506+32027 = 4 \cdot 506 + 3, so 572027≡573≡49⋅57=2793≡93(mod100)57^{2027} \equiv 57^3 \equiv 49 \cdot 57 = 2793 \equiv 93 \pmod{100}. The last two digits are 9393.

Example
Worked: when the base shares a factor with 100

What are the last two digits of 21002^{100}?

Spotting it. gcd⁡(2,100)≠1\gcd(2, 100) \ne 1, so Euler's theorem does not apply mod 100100. Split 100=4⋅25100 = 4 \cdot 25 into coprime parts, work in each, and recombine with CRT.

Step 1. Mod 44: 2100≡02^{100} \equiv 0.

Step 2. Mod 2525: gcd⁡(2,25)=1\gcd(2, 25) = 1 and φ(25)=20\varphi(25) = 20, so 2100=(220)5≡12^{100} = (2^{20})^5 \equiv 1.

Step 3. Find x≡0(mod4)x \equiv 0 \pmod 4 with x≡1(mod25)x \equiv 1 \pmod{25}: among 1,26,51,761, 26, 51, 76 only 7676 is a multiple of 44. The last two digits are 7676.

Side note
Where it goes wrong

Using Euler when the base and modulus share a factor. "φ(100)=40\varphi(100) = 40, so 240≡1(mod100)2^{40} \equiv 1 \pmod{100}" is false: 2402^{40} is even, so it cannot be 11 more than a multiple of 100100 (in fact 240≡762^{40} \equiv 76). Anyone who then writes 2120=(240)3≡12^{120} = (2^{40})^3 \equiv 1 is wrong; 2120≡762^{120} \equiv 76. Split the modulus instead.

Try it
Try it: a last digit modulo 9

What is 7100 mod 97^{100} \bmod 9?

Show the answer

φ(9)=6\varphi(9) = 6 and 100=6⋅16+4100 = 6\cdot16 + 4, so 7100≡74(mod9)7^{100} \equiv 7^4 \pmod 9. Since 72=49≡47^2 = 49 \equiv 4, 74≡16≡7(mod9)7^4 \equiv 16 \equiv 7 \pmod 9.

Squares, Remainders and Impossibility

Tip
Squares have few remainders
n mod 801234567n² mod 801410141
Squares mod 8 take only the values 0, 1 and 4.

Every square is 00 or 1(mod4)1 \pmod 4, and 00, 11 or 4(mod8)4 \pmod 8; mod 33 it is 00 or 11. To prove that an equation has NO integer solutions, find a modulus in which the two sides can never agree.

Why it works. Only the remainder of nn matters, so check each residue once: (2k)2=4k2≡0(mod4)(2k)^2 = 4k^2 \equiv 0 \pmod 4, and (2k+1)2=4k(k+1)+1≡1(mod8)(2k + 1)^2 = 4k(k + 1) + 1 \equiv 1 \pmod 8 because one of k,k+1k, k + 1 is even. An equation that holds for integers must hold mod every nn, so a single modulus where it fails rules out every integer solution at once. A modulus can prove impossibility; it can never prove that a solution exists.

Example
Worked: a sum of two squares

Can 20272027 be written as the sum of two perfect squares?

Spotting it. A sum of two squares is a sum of two numbers from {0,1}\{0, 1\} mod 44, so it is 00, 11 or 2(mod4)2 \pmod 4.

Step 1. 2027=4⋅506+3≡3(mod4)2027 = 4 \cdot 506 + 3 \equiv 3 \pmod 4.

Step 2. No: 33 is not a possible remainder.

Example
Worked: differences of squares

How many of the integers 1,2,…,1001, 2, \dots, 100 can be written as a2−b2a^2 - b^2 with aa and bb nonnegative integers?

Spotting it. a2−b2=(a−b)(a+b)a^2 - b^2 = (a - b)(a + b), and the two factors a−ba - b and a+ba + b have the same parity (they differ by 2b2b). That decides which nn are possible.

Step 1. If nn is odd, n=(n+12)2−(n−12)2n = \left(\tfrac{n + 1}{2}\right)^2 - \left(\tfrac{n - 1}{2}\right)^2. If 4∣n4 \mid n, n=(n4+1)2−(n4−1)2n = \left(\tfrac n4 + 1\right)^2 - \left(\tfrac n4 - 1\right)^2.

Step 2. If n≡2(mod4)n \equiv 2 \pmod 4: two factors of the same parity multiply to an odd number or to a multiple of 44, never to 2(mod4)2 \pmod 4. So these nn fail.

Step 3. The failures are 2,6,10,…,982, 6, 10, \dots, 98, which is 2525 numbers, so 100−25=75100 - 25 = 75 can be written.

Example
Worked: three squares and mod 8

Exactly one of 20232023, 20242024, 20252025, 20262026 cannot be written as a sum of three perfect squares. Which one?

Spotting it. Mod 44 is too coarse for three squares (three terms from {0,1}\{0, 1\} reach every residue). Mod 88 squares are 0,1,40, 1, 4, and three of them can make every residue EXCEPT 77: sums of three values from {0,1,4}\{0, 1, 4\} give 0,1,2,3,4,5,60, 1, 2, 3, 4, 5, 6 and then 8,9,128, 9, 12.

Step 1. 2023=8⋅252+7≡7(mod8)2023 = 8 \cdot 252 + 7 \equiv 7 \pmod 8, so 20232023 is impossible.

Step 2. The others do work, for instance 2024=382+242+222024 = 38^2 + 24^2 + 2^2, 2025=452+02+022025 = 45^2 + 0^2 + 0^2 and 2026=452+12+022026 = 45^2 + 1^2 + 0^2. (The mod-88 test alone could not have shown that; examples do.)

Side note
Where it goes wrong

Treating a passed test as a proof. 21≡1(mod4)21 \equiv 1 \pmod 4 passes the mod-44 test for a sum of two squares, yet 2121 is NOT a sum of two squares: the squares below it are 0,1,4,9,160, 1, 4, 9, 16, and no two of them add to 2121. A modulus can only rule things out. To show something IS possible, exhibit it.

Try it
Try it: no solutions

Show that x2−3y2=2x^2 - 3y^2 = 2 has no solutions in integers.

Show the answer

Work mod 33: the equation becomes x2≡2(mod3)x^2 \equiv 2 \pmod 3. But squares are 00 or 1(mod3)1 \pmod 3 (02=00^2 = 0, 12=11^2 = 1, 22=4≡12^2 = 4 \equiv 1). So there is no solution.