The Setup
Pick any prime number p.
Pick any number a that’s not divisible by p.
The Theorem
Raise a to the power (p−1). Divide by p.
The remainder is always 1.
In notation:
ap−1modp=1
Example
Let p=7 (prime) and a=3.
37−1=36=729
729÷7=104 remainder 1
36mod7=1 ✓
Another Example
Let p=5 and a=2.
25−1=24=16
16÷5=3 remainder 1
24mod5=1 ✓
Why It’s Useful
Imagine you need to compute 3100mod7.
That’s a huge number. But we know 36mod7=1.
Rewrite the exponent:
100=6×16+4
So:
3100=36×16+4=(36)16×34
Since 36mod7=1:
=116×34=81
81mod7=4
Instead of computing 3100, we only needed 34.