Discrete MathematicsModular Arithmetic and Cryptography5 of 6
Modular Arithmetic and Cryptography · 5

Fermat's Little Theorem, Euler's Theorem and Fast Exponentiation

We know how to reduce numbers modulo nn, and now we need a way to compute huge powers like 31000(mod10)3^{\reach{1000}} \pmod{10} without multiplying a thousand times in a row.

1

Fast Exponentiation

To find a remainder like 313(mod10)3^{\reach{13}} \pmod{10} (the last digit of 3133^{\reach{13}}), we can double the exponent at each step by repeatedly squaring, rather than multiplying by 33 one step at a time.

Fast Exponentiation
313≡?(mod10)3^{\htmlClass{lc-pink}{13}} \equiv {?} \pmod{10}
00
11
22
33
44
55
66
77
88
99
1010
1111
1212
13\htmlClass{lc-pink}{13}
1414
Text solution
Break the exponent into powers of 22Write 13\reach{13} in binary to split 3133^{\reach{13}} into a product of powers of 33 whose exponents are powers of 22.
13=11012=8+4+1  ⟹  313=38⋅34⋅31\displaystyle \begin{aligned} \reach{13} &= 1101_2 = \piece{8} + \piece{4} + \piece{1} \\ \implies 3^{\reach{13}} &= 3^{\piece{8}} \cdot 3^{\piece{4}} \cdot 3^{\piece{1}} \end{aligned}
Square repeatedly modulo 1010Double the exponent on each line by squaring the previous remainder and reducing modulo 1010.
31=332=934=(32)2=81≡1(mod10)38=(34)2≡12=1(mod10)\displaystyle \begin{aligned} 3^{\piece{1}} &= 3 \\ 3^2 &= 9 \\ 3^{\piece{4}} &= (3^2)^{\squaring{2}} = 81 \equiv 1 \pmod{10} \\ 3^{\piece{8}} &= (3^{\piece{4}})^{\squaring{2}} \equiv 1^{\squaring{2}} = 1 \pmod{10} \end{aligned}
Multiply the matching piecesSubstitute the reduced remainders for 383^{\piece{8}}, 343^{\piece{4}}, and 313^{\piece{1}} to find the remainder of 3133^{\reach{13}}.
313\displaystyle 3^{\reach{13}} =38⋅34⋅31≡1⋅1⋅3\displaystyle = 3^{\piece{8}} \cdot 3^{\piece{4}} \cdot 3^{\piece{1}} \equiv 1 \cdot 1 \cdot 3 =3(mod10)\displaystyle = 3 \pmod{10}

So the last digit of 3133^{\reach{13}} is 33.

Every exponent k\reach{k} breaks into a sum of powers of 22 in binary, and multiplying the matching rows from the squaring table modulo nn gives 3k(modn)3^{\reach{k}} \pmod n in just a few steps:

Because every exponent breaks into a sum of powers of two, you can build any power of three up to the 63rd just by multiplying the matching rows from a six-row squaring table.
2

Fermat's Little Theorem

In the squaring table for 313(mod10)3^{\reach{13}} \pmod{10}, the remainders hit 11 at 34≡1(mod10)3^{\reset{4}} \equiv 1 \pmod{10} and started repeating. When the modulus is a prime pp, we can predict when the powers hit 11 ahead of time: the powers of any integer aa not divisible by pp are guaranteed to equal 11 at ap−1≡1(modp)a^{\reset{p-1}} \equiv 1 \pmod p.

Fermat's Little Theorem
(mod7)\pmod{7}
1⋅3≡1 \cdot \htmlClass{lc-violet}{3} \equiv
2⋅3≡2 \cdot \htmlClass{lc-violet}{3} \equiv
3⋅3≡3 \cdot \htmlClass{lc-violet}{3} \equiv
4⋅3≡4 \cdot \htmlClass{lc-violet}{3} \equiv
5⋅3≡5 \cdot \htmlClass{lc-violet}{3} \equiv
6⋅3≡6 \cdot \htmlClass{lc-violet}{3} \equiv

For every non-zero remainder a\mult{a} modulo a prime pp, multiplying the p−1\reset{p - 1} non-zero remainders by a\mult{a} gives back the same remainders in some order, with no 00 and no repeats, so their product cancels on both sides:

Interactive c2

Cancelling the product 1⋅2⋯(p−1)\cancelled{1 \cdot 2 \cdots (p - 1)} from both sides leaves p−1\reset{p - 1} copies of a\mult{a} equal to 11 modulo pp:

(1⋅2⋯(p−1))⋅ap−1≡1⋅2⋯(p−1)(modp)  ⟹  ap−1≡1(modp)⏟Fermat’s Little Theorem(gcd⁡(a,p)=1)\displaystyle \begin{aligned} &\cancelled{(1 \cdot 2 \cdots (p - 1))} \cdot \mult{a}^{\reset{p-1}} \\ &\qquad \equiv \cancelled{1 \cdot 2 \cdots (p - 1)} \pmod p \\ &\implies \underbrace{\mult{a}^{\reset{p-1}} \equiv 1 \pmod p}_{\text{Fermat's Little Theorem}} \\ &\qquad (\gcd(\mult{a}, p) = 1) \end{aligned}

Keep in mind that the powers are guaranteed to equal 11 at p−1\reset{p - 1}, even if they hit 11 earlier: modulo 77, 23=8≡1(mod7)2^3 = 8 \equiv 1 \pmod 7 hits 11 after just 33 steps, then repeats that 33-step cycle to land back on 11 at 26≡1(mod7)2^{\reset{6}} \equiv 1 \pmod 7.

Let's use 36≡1(mod7)3^{\reset{6}} \equiv 1 \pmod 7 to shrink the exponent in 3100(mod7)3^{\reach{100}} \pmod 7.

Text solution
Find the exponent that gives 11 modulo 77Since 77 is prime and gcd⁡(3,7)=1\gcd(3, 7) = 1, Fermat's Little Theorem guarantees that 36≡1(mod7)3^{\reset{6}} \equiv 1 \pmod 7.
37−1\displaystyle 3^{\reset{7-1}} =36≡1(mod7)\displaystyle = 3^{\reset{6}} \equiv 1 \pmod 7
Divide the exponent by 6\reset{6}Write 100\reach{100} as a multiple of 6\reset{6} plus a remainder so every 363^{\reset{6}} turns into 11.
100=6⋅16+4  ⟹  3100=(36)16⋅34≡116⋅34=34(mod7)\displaystyle \begin{aligned} \reach{100} &= \reset{6} \cdot 16 + \leftover{4} \\ \implies 3^{\reach{100}} &= (3^{\reset{6}})^{16} \cdot 3^{\leftover{4}} \\ &\equiv 1^{16} \cdot 3^{\leftover{4}} \\ &= 3^{\leftover{4}} \pmod 7 \end{aligned}
Reduce the remaining powerCompute 343^{\leftover{4}} and find its remainder modulo 77.
34\displaystyle 3^{\leftover{4}} =81\displaystyle = 81 =7⋅11+4≡4(mod7)\displaystyle = 7 \cdot 11 + 4 \equiv 4 \pmod 7

So 31003^{\reach{100}} leaves a remainder of 44 when divided by 77.

3

Proving Divisibility for All Integers

The congruence ap−1≡1(modp)a^{\reset{p-1}} \equiv 1 \pmod p leaves out multiples of pp, where 0p−1≡0≢1(modp)0^{\reset{p-1}} \equiv \fails{0} \not\equiv 1 \pmod p. When we need to prove a divisibility statement for every integer nn at once, we multiply both sides by aa so that multiples of pp work too.

Proving Divisibility for All Integers
n5−n≡?(mod30)\displaystyle n^5 - n \equiv {?} \pmod{30}
for every integer n\displaystyle \text{for every integer } n

Multiplying ap−1≡1(modp)a^{\reset{p-1}} \equiv 1 \pmod p by aa gives a statement with no exceptions, since 0p≡0(modp)0^p \equiv 0 \pmod p is also true:

ap≡a(modp)for every integer a\displaystyle a^p \equiv a \pmod p \quad \text{for every integer } a

Combined with the Chinese Remainder Theorem (which says that pairwise coprime numbers divide a number together if and only if each one divides it separately), this form proves divisibility across all integers without splitting into cases.

Text solution
Factor the modulus into distinct primesSince 30=2⋅3⋅530 = \ptwo{2} \cdot \pthree{3} \cdot \pfive{5} is a product of pairwise coprime primes, 3030 divides n5−nn^5 - n if and only if 2\ptwo{2}, 3\pthree{3}, and 5\pfive{5} each divide n5−nn^5 - n.
30\displaystyle 30 =2⋅3⋅5\displaystyle = \ptwo{2} \cdot \pthree{3} \cdot \pfive{5}
Reduce n5−nn^5 - n modulo each prime using np≡n(modp)n^p \equiv n \pmod pReplace n5n^{\pfive{5}} with nn modulo 5\pfive{5}, n3n^{\pthree{3}} with nn modulo 3\pthree{3}, and n2n^{\ptwo{2}} with nn modulo 2\ptwo{2}.
Mod 5:n5−n≡n−n=0(mod5)Mod 3:n5−n=n3⋅n2−n≡n⋅n2−n=n3−n≡n−n=0(mod3)Mod 2:n5−n≡n−n=0(mod2)\displaystyle \begin{aligned} \text{Mod } \pfive{5}: \quad & \\ n^5 - n &\equiv n - n = 0 \pmod{\pfive{5}} \\ \text{Mod } \pthree{3}: \quad & \\ n^5 - n &= n^{\pthree{3}} \cdot n^2 - n \\ &\equiv n \cdot n^2 - n \\ &= n^{\pthree{3}} - n \\ &\equiv n - n = 0 \pmod{\pthree{3}} \\ \text{Mod } \ptwo{2}: \quad & \\ n^5 - n &\equiv n - n = 0 \pmod{\ptwo{2}} \end{aligned}

Because n5−nn^5 - n is a multiple of 2\ptwo{2}, 3\pthree{3}, and 5\pfive{5} for every integer nn, it is divisible by 3030 for every integer nn.

4

Fermat's Primality Test and Pseudoprimes

Suppose we are given a huge odd number nn and want to know whether nn is prime without factoring it. We can use Fermat's Little Theorem as a test by computing an−1(modn)a^{n-1} \pmod n with fast exponentiation.

Fermat's Primality Test and Pseudoprimes
n odd prime  ⟹  2n−1≡1(modn)\htmlClass{lc-blue}{n \text{ odd prime}} \implies \htmlClass{lc-pink}{2^{n-1} \equiv 1 \pmod n}
is n prime?\text{is } n \text{ prime?}
2n−1≡1\htmlClass{lc-pink}{2^{n-1} \equiv 1}
n prime\htmlClass{lc-blue}{n \text{ prime}}
odd n, gcd⁡(a,n)=1\text{odd } n, \ \gcd(a, n) = 1
an−1≡1a^{n-1} \htmlClass{lc-green}{{}\equiv 1}
(mod n)(\mathrm{mod}\ n)
all primes\text{all primes}
33
55
77
1111
⋯\cdots
some composites\text{some composites}
341\htmlClass{lc-orange}{341}
a=2a = 2
561\htmlClass{lc-orange}{561}
every a\text{every } a
an−1≢1a^{n-1} \htmlClass{lc-orange}{{}\not\equiv 1}
(mod n)(\mathrm{mod}\ n)
composites\text{composites}
1515
a=2a = 2

Let's compare how the test behaves on n=15n = 15 and n=341n = 341 using base a=2a = 2.

Text solution
Test n=15n = 15 by computing 214(mod15)2^{\reach{14}} \pmod{15}Square 22 repeatedly modulo 1515 and multiply the rows for 14=8+4+2\reach{14} = \piece{8} + \piece{4} + \piece{2}.
21=2,22=4,24=16≡1,28≡1(mod15)\displaystyle \begin{aligned} 2^1 &= 2, \\ 2^{\piece{2}} &= 4, \\ 2^{\piece{4}} &= 16 \equiv 1, \\ 2^{\piece{8}} &\equiv 1 \pmod{15} \end{aligned}
214=28⋅24⋅22≡1⋅1⋅4=4≢1(mod15)\displaystyle \begin{aligned} 2^{\reach{14}} &= 2^{\piece{8}} \cdot 2^{\piece{4}} \cdot 2^{\piece{2}} \\ &\equiv 1 \cdot 1 \cdot 4 = \fails{4} \not\equiv 1 \pmod{15} \end{aligned}
Test n=341n = 341 by computing 2340(mod341)2^{\reach{340}} \pmod{341}Use 210=10242^{\reset{10}} = 1024 =341⋅3+1= 341 \cdot 3 + 1 ≡1(mod341)\equiv 1 \pmod{341} to evaluate 23402^{\reach{340}}.
2340\displaystyle 2^{\reach{340}} =(210)34≡134\displaystyle = (2^{\reset{10}})^{34} \equiv 1^{34} =1(mod341)\displaystyle = \passes{1} \pmod{341}

Since 214≢1(mod15)2^{\reach{14}} \fails{{}\not\equiv 1} \pmod{15}, the contrapositive of Fermat's Little Theorem proves 1515 is composite without finding any of its factors. Since 2340≡1(mod341)2^{\reach{340}} \equiv \passes{1} \pmod{341}, the test is inconclusive for 341341. In fact, 341=11⋅31\fails{341} = 11 \cdot 31 is a composite number that passes the test for base 22 (called a pseudoprime to base 22), though base 33 catches it because 3340≡56≢1(mod341)3^{\reach{340}} \equiv \fails{56} \not\equiv 1 \pmod{341}. Rare composite numbers called Carmichael numbers, like 561\fails{561}, pass the test for every base coprime to nn.

5

Euler's Theorem and Euler's Totient Function

Now suppose we already know the modulus nn is composite, such as n=10n = 10 or n=100n = 100, and we want to shrink a huge exponent. We can't cancel the product of all the remainders 1,…,n−11, \dots, n - 1 because numbers that share a factor with nn have no modular inverse, so we keep only the remainders that are coprime to nn.

Euler's Theorem and Euler's Totient Function
(mod10)\pmod{10}
11
22
33
44
55
66
77
88
99
3?≡1(mod10)3^{?} \equiv 1 \pmod{10}

Crossing out the multiples of each prime factor pp of nn removes 1p\frac{1}{p} of the list at each step, leaving the ϕ(n)\reset{\phi(n)} coprime remainders. Multiplying these ϕ(n)\reset{\phi(n)} remainders by a coprime base a\mult{a} just rearranges them:

Crossing out the multiples of each prime factor of 12 shrinks the list by that fraction and leaves the four coprime remainders whose product cancels.

For any modulus n>1n > 1, we find ϕ(n)\reset{\phi(n)} from the distinct prime factors pp of nn, and use aϕ(n)≡1(modn)a^{\reset{\phi(n)}} \equiv 1 \pmod n whenever gcd⁡(a,n)=1\gcd(a, n) = 1:

ϕ(n)=n∏p∣n(1−1p)⏟Euler’s totient functionaϕ(n)≡1(modn)⏟Euler’s Theorem(gcd⁡(a,n)=1)\displaystyle \begin{gathered} \underbrace{\reset{\phi(n)} = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right)}_{\text{Euler's totient function}} \\ \underbrace{a^{\reset{\phi(n)}} \equiv 1 \pmod n}_{\text{Euler's Theorem}} \quad (\gcd(a, n) = 1) \end{gathered}

When n=pn = p is prime, no number from 11 to p−1p - 1 shares a factor with pp, so ϕ(p)=p(1−1p)=p−1\reset{\phi(p)} = p\left(1 - \frac{1}{p}\right) = \reset{p - 1} and Euler's Theorem becomes Fermat's Little Theorem.

Text solution
Find the last digit of 310003^{\reach{1000}} using ϕ(10)\reset{\phi(10)}The remainders coprime to 1010 are 1,3,7,9\coprime{1, 3, 7, 9}, so ϕ(10)=4\reset{\phi(10)} = \reset{4}, and since gcd⁡(3,10)=1\gcd(3, 10) = 1, 34≡1(mod10)3^{\reset{4}} \equiv 1 \pmod{10}.
31000\displaystyle 3^{\reach{1000}} =(34)250≡1250\displaystyle = (3^{\reset{4}})^{250} \equiv 1^{250} =1(mod10)\displaystyle = 1 \pmod{10}
Count the remainders coprime to 100100Factor 100=22⋅52100 = \ptwo{2}^2 \cdot \pfive{5}^2 to find its distinct prime factors 2\ptwo{2} and 5\pfive{5}, then multiply 100100 by (1−12)\ptwo{\left(1 - \frac{1}{2}\right)} and (1−15)\pfive{\left(1 - \frac{1}{5}\right)}.
ϕ(100)=100⋅(1−12)(1−15)=100⋅12⋅45=40\displaystyle \begin{aligned} \reset{\phi(100)} &= 100 \cdot \ptwo{\left(1 - \frac{1}{2}\right)}\pfive{\left(1 - \frac{1}{5}\right)} \\ &= 100 \cdot \ptwo{\frac{1}{2}} \cdot \pfive{\frac{4}{5}} = \reset{40} \end{aligned}

So the last digit of 310003^{\reach{1000}} is 11, and any base aa coprime to 100100 satisfies a40≡1(mod100)a^{\reset{40}} \equiv 1 \pmod{100}.

6

Combining Euler's Theorem and Fast Exponentiation

When we want the last two digits of 720267^{\reach{2026}}, which means finding 72026(mod100)7^{\reach{2026}} \pmod{100}, dividing the exponent by ϕ(100)=40\reset{\phi(100)} = \reset{40} still leaves a remainder of 26\leftover{26}. Whenever the reduced exponent is still too big to compute in one step, use Euler's Theorem first to shrink the exponent and fast exponentiation second to finish the calculation.

Combining Euler's Theorem and Fast Exponentiation
72026≡?(mod100)\displaystyle 7^{\reach{2026}} \equiv {?} \pmod{100}
Text solution
Shrink the exponent modulo ϕ(100)=40\reset{\phi(100)} = \reset{40}Since gcd⁡(7,100)=1\gcd(7, 100) = 1, 740≡1(mod100)7^{\reset{40}} \equiv 1 \pmod{100}, so divide 2026\reach{2026} by 40\reset{40} and keep the remainder 26\leftover{26}.
2026=40⋅50+26  ⟹  72026=(740)50⋅726≡150⋅726=726(mod100)\displaystyle \begin{aligned} \reach{2026} &= \reset{40} \cdot 50 + \leftover{26} \\ \implies 7^{\reach{2026}} &= (7^{\reset{40}})^{50} \cdot 7^{\leftover{26}} \\ &\equiv 1^{50} \cdot 7^{\leftover{26}} \\ &= 7^{\leftover{26}} \pmod{100} \end{aligned}
Split the remaining exponent into powers of 22Break 26\leftover{26} into powers of 22 for repeated squaring.
26=16+8+2  ⟹  726=716⋅78⋅72\displaystyle \begin{aligned} \leftover{26} &= \piece{16} + \piece{8} + \piece{2} \\ \implies 7^{\leftover{26}} &= 7^{\piece{16}} \cdot 7^{\piece{8}} \cdot 7^{\piece{2}} \end{aligned}
Square repeatedly modulo 100100 and multiplyCompute the squaring table up to 716(mod100)7^{\piece{16}} \pmod{100} and multiply the needed pieces.
71=772=4974=492=2401≡1(mod100)78≡12=1,716≡12=1(mod100)\displaystyle \begin{aligned} 7^1 &= 7 \\ 7^{\piece{2}} &= 49 \\ 7^4 &= 49^{\squaring{2}} = 2401 \equiv 1 \pmod{100} \\ 7^{\piece{8}} &\equiv 1^{\squaring{2}} = 1, \\ 7^{\piece{16}} &\equiv 1^{\squaring{2}} = 1 \pmod{100} \end{aligned}
726\displaystyle 7^{\leftover{26}} =716⋅78⋅72≡1⋅1⋅49\displaystyle = 7^{\piece{16}} \cdot 7^{\piece{8}} \cdot 7^{\piece{2}} \equiv 1 \cdot 1 \cdot 49 =49(mod100)\displaystyle = 49 \pmod{100}

So the last two digits of 720267^{\reach{2026}} are 4949.

Key concepts

To compute a modular power ak(modn)a^{\reach{k}} \pmod n, start by checking whether you can shrink the exponent k\reach{k} before you multiply. If gcd⁡(a,n)=1\gcd(a, n) = 1, Euler's Theorem tells us that aϕ(n)≡1(modn)a^{\reset{\phi(n)}} \equiv 1 \pmod n, where ϕ(n)=n∏p∣n(1−1p)\reset{\phi(n)} = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right) uses each distinct prime factor pp of nn once (for a prime modulus pp, this is Fermat's Little Theorem, ap−1≡1(modp)a^{\reset{p-1}} \equiv 1 \pmod p, as in 36≡1(mod7)3^{\reset{6}} \equiv 1 \pmod 7). Divide k\reach{k} by ϕ(n)\reset{\phi(n)} and keep only the remainder of the exponent, as in 72026≡726(mod100)7^{\reach{2026}} \equiv 7^{\leftover{26}} \pmod{100} since ϕ(100)=40\reset{\phi(100)} = \reset{40}.

Do not use Euler's or Fermat's Theorem to reduce the exponent if gcd⁡(a,n)≠1\gcd(a, n) \neq 1 or if the exponent k\reach{k} is already smaller than ϕ(n)\reset{\phi(n)} (such as 513(mod23)5^{\reach{13}} \pmod{23}, where 13<22\reach{13} < \reset{22}). In those cases, and whenever reducing modulo ϕ(n)\reset{\phi(n)} still leaves an exponent like 26\leftover{26} in 726(mod100)7^{\leftover{26}} \pmod{100}, use fast exponentiation: split the exponent into powers of 22 (13=8+4+1\reach{13} = \piece{8} + \piece{4} + \piece{1}), square and reduce modulo nn row by row, and multiply the matching rows. For two special questions, use Fermat's Little Theorem in a slightly different way: to prove a prime pp divides an expression for every integer nn (including multiples of pp), use np≡n(modp)n^p \equiv n \pmod p, which holds for all integers; and to test if an odd number nn is prime, check an−1(modn)a^{n-1} \pmod n for a base aa with gcd⁡(a,n)=1\gcd(a, n) = 1. Getting anything other than 11 proves nn is composite (214≡4≢1(mod15)2^{\reach{14}} \equiv \fails{4} \not\equiv 1 \pmod{15}), while getting 11 is inconclusive because some composite numbers (2340≡1(mod341)2^{\reach{340}} \equiv \passes{1} \pmod{\fails{341}}) also give 11.

7

Practice

1

Use fast exponentiation (repeated squaring) to compute 513(mod23)5^{\reach{13}} \pmod{23}.

Since 13\reach{13} is already smaller than 22\reset{22}, go straight to fast exponentiation: how do you write 13\reach{13} as a sum of powers of 22?

Write 13=8+4+1\reach{13} = \piece{8} + \piece{4} + \piece{1}. Repeated squaring modulo 2323 gives
51≡5,52=25≡2,54≡22=4,58≡42=16(mod23)\displaystyle \begin{aligned} 5^{\piece{1}} &\equiv 5, \\ 5^2 &= 25 \equiv 2, \\ 5^{\piece{4}} &\equiv 2^{\squaring{2}} = 4, \\ 5^{\piece{8}} &\equiv 4^{\squaring{2}} = 16 \pmod{23} \end{aligned}
(Warning: when moving from 54≡45^{\piece{4}} \equiv 4 to 585^{\piece{8}}, square the remainder to get 42=164^{\squaring{2}} = 16; doubling the remainder to 4⋅2=84 \fails{{}\cdot 2} = 8 gives the wrong answer 22\fails{22}, and adding the remainders 16+4+5=25≡216 + 4 + 5 = 25 \equiv \fails{2} instead of multiplying them gives the wrong answer 2\fails{2}.) Multiplying the pieces together gives
513=58⋅54⋅51≡16⋅4⋅5=320=23⋅13+21≡21(mod23)\displaystyle \begin{aligned} 5^{\reach{13}} &= 5^{\piece{8}} \cdot 5^{\piece{4}} \cdot 5^{\piece{1}} \\ &\equiv 16 \cdot 4 \cdot 5 \\ &= 320 = 23 \cdot 13 + 21 \\ &\equiv 21 \pmod{23} \end{aligned}
2121
2

Use Fermat's Little Theorem to find the remainder when 210002^{\reach{1000}} is divided by 1313.

Since 1313 is prime and gcd⁡(2,13)=1\gcd(2, 13) = 1, what power of 22 is guaranteed to be 1(mod13)1 \pmod{13}?

Since 1313 is prime and gcd⁡(2,13)=1\gcd(2, 13) = 1, Fermat's Little Theorem gives 212≡1(mod13)2^{\reset{12}} \equiv 1 \pmod{13}. Dividing the exponent 1000\reach{1000} by 12\reset{12} gives 1000=12⋅83+4\reach{1000} = \reset{12} \cdot 83 + \leftover{4} (warning: divide the exponent by p−1=12\reset{p - 1} = \reset{12}, not by the modulus 1313, which would leave remainder 12\fails{12} and give the wrong answer 1\fails{1}). Reducing the power gives
21000\displaystyle 2^{\reach{1000}} =(212)83⋅24≡183⋅16≡3(mod13)\displaystyle = (2^{\reset{12}})^{83} \cdot 2^{\leftover{4}} \equiv 1^{83} \cdot 16 \equiv 3 \pmod{13}
(Warning: don't stop at the reduced exponent 4\leftover{4}. Remember to evaluate 24=16≡3(mod13)2^{\leftover{4}} = 16 \equiv 3 \pmod{13}.) 33
3

Prove that 4242 divides n7−nn^7 - n for every integer nn.

Factor 4242 into primes, and use the form of Fermat's Little Theorem that holds for all integers, including multiples of pp.

Factor 42=2⋅3⋅742 = \ptwo{2} \cdot \pthree{3} \cdot \pseven{7}. By Fermat's Little Theorem in the form np≡n(modp)n^p \equiv n \pmod p, which holds for every integer nn (warning: do not write np−1≡1(modp)n^{\reset{p-1}} \equiv 1 \pmod p without checking whether p∣np \mid n, because that form fails when n≡0(modp)n \equiv \fails{0} \pmod p):
Mod 7\pseven{7}: n7≡n(mod7)n^{\pseven{7}} \equiv n \pmod{\pseven{7}}, so n7−n≡n−n=0(mod7)n^7 - n \equiv n - n = 0 \pmod{\pseven{7}}.
Mod 3\pthree{3}: n3≡n(mod3)n^{\pthree{3}} \equiv n \pmod{\pthree{3}}, so n7=(n3)2⋅nn^7 = (n^{\pthree{3}})^2 \cdot n ≡n2⋅n\equiv n^2 \cdot n =n3= n^{\pthree{3}} ≡n(mod3)\equiv n \pmod{\pthree{3}}, which gives n7−n≡0(mod3)n^7 - n \equiv 0 \pmod{\pthree{3}}.
Mod 2\ptwo{2}: n2≡n(mod2)n^{\ptwo{2}} \equiv n \pmod{\ptwo{2}}, so repeated replacements of n2n^{\ptwo{2}} with nn give n7≡n(mod2)n^7 \equiv n \pmod{\ptwo{2}} and n7−n≡0(mod2)n^7 - n \equiv 0 \pmod{\ptwo{2}}.
Since the pairwise coprime primes 2,3,\ptwo{2}, \pthree{3}, and 7\pseven{7} each divide n7−nn^7 - n, the Chinese Remainder Theorem guarantees that 4242 divides n7−nn^7 - n for every integer nn.
4

Test n=65n = 65 with Fermat's primality test using base a=8a = 8 and base a=2a = 2 by computing 864(mod65)8^{\reach{64}} \pmod{65} and 264(mod65)2^{\reach{64}} \pmod{65} via repeated squaring. What does each result tell you about 6565?

Square each base repeatedly up to the 64th\reach{64}\text{th} power modulo 6565; what can you conclude when a64(mod65)a^{\reach{64}} \pmod{65} equals 11 versus when it does not equal 11?

For base a=8a = 8, repeated squaring modulo 6565 gives 82=648^2 = 64 and 84=6428^4 = 64^{\squaring{2}} =4096= 4096 =65⋅63+1= 65 \cdot 63 + 1 ≡1(mod65)\equiv 1 \pmod{65}, so 88,816,832,8^8, 8^{16}, 8^{32}, and 8648^{\reach{64}} are all congruent to 1(mod65)\passes{1} \pmod{65}. Because the converse of Fermat's Little Theorem is false, this test is inconclusive (6565 could be prime or composite; warning: do not conclude from 864≡1(mod65)8^{\reach{64}} \equiv \passes{1} \pmod{65} that 6565 is prime).
For base a=2a = 2, repeated squaring modulo 6565 gives
21≡2,22≡4,24≡16,28=256=65⋅3+61≡61(mod65)\displaystyle \begin{aligned} 2^1 &\equiv 2, \\ 2^2 &\equiv 4, \\ 2^4 &\equiv 16, \\ 2^8 &= 256 = 65 \cdot 3 + 61 \\ &\equiv 61 \pmod{65} \end{aligned}
216≡612=3721=65⋅57+16≡16,232≡162≡61,264≡612≡16≢1(mod65)\displaystyle \begin{aligned} 2^{16} &\equiv 61^{\squaring{2}} = 3721 \\ &= 65 \cdot 57 + 16 \equiv 16, \\ 2^{32} &\equiv 16^{\squaring{2}} \equiv 61, \\ 2^{\reach{64}} &\equiv 61^{\squaring{2}} \equiv \fails{16} \not\equiv 1 \pmod{65} \end{aligned}
By the contrapositive of Fermat's Little Theorem, 264≢1(mod65)2^{\reach{64}} \fails{{}\not\equiv 1} \pmod{65} proves for sure that 6565 is composite.
5

Compute ϕ(180)\reset{\phi(180)}, the number of integers from 11 to 180180 that are coprime to 180180.

Find the prime factorization of 180180 first; which distinct primes divide 180180?

Since 180=22⋅32⋅5180 = \ptwo{2}^2 \cdot \pthree{3}^2 \cdot \pfive{5}, its distinct prime factors are 2,3,\ptwo{2}, \pthree{3}, and 5\pfive{5}. (Warning: include each distinct prime factor only once in the product. Multiplying by (1−12)\ptwo{\left(1 - \frac{1}{2}\right)} and (1−13)\pthree{\left(1 - \frac{1}{3}\right)} twice because of the squares 22\ptwo{2}^2 and 32\pthree{3}^2 gives the wrong answer 16\fails{16}.) Therefore,
ϕ(180)=180⋅(1−12)(1−13)(1−15)=180⋅12⋅23⋅45=48\displaystyle \begin{aligned} \reset{\phi(180)} &= 180 \cdot \ptwo{\left(1 - \frac{1}{2}\right)} \\ &\qquad \pthree{\left(1 - \frac{1}{3}\right)}\pfive{\left(1 - \frac{1}{5}\right)} \\ &= 180 \cdot \ptwo{\frac{1}{2}} \cdot \pthree{\frac{2}{3}} \cdot \pfive{\frac{4}{5}} = 48 \end{aligned}
4848
6

Find the last two digits of 320273^{\reach{2027}}.

Finding the last two digits means working modulo 100100: use ϕ(100)\reset{\phi(100)} to shrink the exponent 2027\reach{2027} first, then break the remaining exponent into powers of 22.

Working modulo 100100, since gcd⁡(3,100)=1\gcd(3, 100) = 1 and ϕ(100)=40\reset{\phi(100)} = \reset{40}, Euler's Theorem gives 340≡1(mod100)3^{\reset{40}} \equiv 1 \pmod{100} (warning: 100100 is composite, so do not use 100−1=99100 - 1 = \fails{99} as the exponent that gives 11: in fact 399≡67≢1(mod100)3^{\fails{99}} \equiv \fails{67} \not\equiv 1 \pmod{100}). Because 2027=40⋅50+27\reach{2027} = \reset{40} \cdot 50 + \leftover{27},
32027\displaystyle 3^{\reach{2027}} =(340)50⋅327≡327(mod100)\displaystyle = (3^{\reset{40}})^{50} \cdot 3^{\leftover{27}} \equiv 3^{\leftover{27}} \pmod{100}
Using fast exponentiation with 27=16+8+2+1\leftover{27} = \piece{16} + \piece{8} + \piece{2} + \piece{1} (warning: don't forget the +1\piece{+ 1} for 31=33^{\piece{1}} = 3 when splitting 27\leftover{27}, or you will compute 326≡29(mod100)3^{\fails{26}} \equiv \fails{29} \pmod{100}):
31≡3,32≡9,34≡81,38=812=6561≡61,316≡612=3721≡21(mod100)\displaystyle \begin{aligned} 3^{\piece{1}} &\equiv 3, \\ 3^{\piece{2}} &\equiv 9, \\ 3^4 &\equiv 81, \\ 3^{\piece{8}} &= 81^{\squaring{2}} = 6561 \equiv 61, \\ 3^{\piece{16}} &\equiv 61^{\squaring{2}} = 3721 \\ &\equiv 21 \pmod{100} \end{aligned}
Multiplying the matching powers gives
327=316⋅38⋅32⋅31≡21⋅61⋅9⋅3=1281⋅27≡81⋅27=2187≡87(mod100)\displaystyle \begin{aligned} 3^{\leftover{27}} &= 3^{\piece{16}} \cdot 3^{\piece{8}} \cdot 3^{\piece{2}} \cdot 3^{\piece{1}} \\ &\equiv 21 \cdot 61 \cdot 9 \cdot 3 = 1281 \cdot 27 \\ &\equiv 81 \cdot 27 = 2187 \\ &\equiv 87 \pmod{100} \end{aligned}
8787