Module 3.1 · Number Theory
Fermat's and Euler's theorems
Almost every AIME has a problem that asks for the remainder of an enormous power, like or a tower such as , when divided by some modulus. You can't compute these numbers, but you don't have to. Fermat's and Euler's theorems tell you that powers repeat, and exactly how often, so a giant exponent shrinks to a small one.
Fermat's little theorem
Fermat's little theorem
If is prime and , then
Equivalently, for every integer (including multiples of ).
Why it's true. Look at the numbers
None of them is divisible by , and no two are congruent mod : if , then , and since we get , which forces . So these numbers are just in some scrambled order mod . Multiply them all together:
Since is not divisible by , you can cancel it, leaving .
How to use it. Mod a prime , exponents only matter mod . To find : , and , so .
Euler's theorem
For a modulus that isn't prime, you need to know how many residues are invertible.
Definition
Euler's totient function
is the number of integers in that are relatively prime to . If , then
For a prime power, : out of numbers, the multiples of are the only ones that fail. For general , is multiplicative: when . That follows from the Chinese remainder theorem (next module): a residue mod is coprime to exactly when its residues mod and mod are coprime to and . Useful values: , , .
Euler's theorem
If , then
The proof is the same as Fermat's. Let be the residues coprime to . Multiplying each by permutes them (same cancellation argument), so , and the product is invertible mod . Fermat is the special case , .
Common mistake
Euler's theorem needs . For you cannot reduce the exponent mod , because . Split instead: , and Euler works mod . Then combine with the Chinese remainder theorem.
Also, is a period, not necessarily the smallest period. Mod , already, even though . Finding the smallest period is the subject of the module on orders.
Wilson's theorem
Wilson's theorem
If is prime, then .
Why. Each of has an inverse mod . The only numbers that are their own inverse satisfy , that is, , so . All the other factors in pair off with their inverses, and each pair multiplies to . What's left is .
Wilson's theorem is how you handle factorials mod a prime: , and in general you peel off the top few factors, which are .
Worked examples
Worked example: Last two digits
Find the last two digits of .
Since and , you have . Now , so .
Compute: , so . (The true period is , even better.) Then .
The last two digits are .
Worked example: A tower
Find the remainder when is divided by .
Mod , , so you only need the exponent mod . Powers of mod cycle with period , and , so .
So . The remainder is .
The pattern for towers: to reduce mod , reduce the exponent mod (or a smaller period), which is again a power problem with a smaller modulus.
Worked example: Wilson's theorem
Find the remainder when is divided by .
By Wilson, . But and , so , giving . The remainder is .
Worked example: A divisibility for all n
Prove that is divisible by for every integer .
. By Fermat, . Mod : , so . Mod : and have the same parity. So , and all divide , and so does their product.
In general, for all whenever .
Tip
Mod , always split into mod and mod . Powers of die mod quickly, and mod you have , so exponents reduce mod .
Practice
Find the remainder when is divided by .
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Find the remainder when is divided by .
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Find the remainder when is divided by .
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Find the remainder when is divided by .
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Find the largest positive integer such that divides for every integer .
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Find the last three digits of .
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Find the sum of all positive integers such that .
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Find the remainder when is divided by .
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Real contest practice
- 2019 AIME I, Problem 14: the least odd prime factor of , using Fermat's theorem with orders.
- 2021 AIME II, Problem 13: a power expression that must be a multiple of ; reduce exponents mod and mod .
- 2010 AIME I, Problem 2: a warm-up in working mod .