Fermat Little Theorem Calculator
Check Fermat's little theorem for a base and prime modulus using modular powers.
What it does
Fermat's little theorem is one of the classic results of number theory. It says that raising any integer to the power p minus 1 leaves remainder 1 when divided by a prime p, provided p does not divide the integer.
This tool takes a base and a prime modulus, verifies the modulus by trial division, and computes both a to the power p minus 1 and a to the power p modulo p using repeated squaring.
The page opens with a = 3 and p = 7. The exponent is 6, and 3 to the sixth power leaves remainder 1 on division by 7. The alternate form 3 to the seventh power leaves remainder 3.
The theorem is used to simplify large modular powers and to find modular inverses. It also underlies a simple compositeness test, because a modulus that violates the congruence for some base cannot be prime.
The converse is not a reliable primality test on its own, since some composite numbers pass for every coprime base. Those numbers are called Carmichael numbers.
How to use
- Enter the base as a whole number.
- Enter a prime modulus.
- Read the main result, a to the power p minus 1 modulo p.
- Check the alternate value of a to the power p modulo p.
- Use the exponent row to see the power that was applied.
- Try a composite modulus to see the prime check reject it.
Formula
- a
- The integer base.
- p
- A prime modulus.
- congruent
- Two numbers are congruent modulo p when they leave the same remainder.
The repeated squaring algorithm reduces the exponent by half at each step and multiplies only the squared values whose binary digit is one. Every intermediate result is reduced modulo p, so no huge power is ever built.
Worked example
Example: base 3, prime 7
The page opens with a = 3 and p = 7.
- The exponent p minus 1 is 6.
- Compute 3 squared modulo 7, which is 2.
- Square again: 2 squared modulo 7 is 4, which is 3 to the fourth power.
- Multiply 3 to the fourth power by 3 squared: 4 times 2 is 8, which reduces to 1 modulo 7.
- The main result is therefore 1, confirming the theorem.
- The alternate value 3 to the seventh power modulo 7 is 3, matching a modulo p.
Reference table
| Base a | Prime p | a to the power p minus 1 mod p | a to the power p mod p |
|---|---|---|---|
| 3 | 7 | 1 | 3 |
| 2 | 13 | 1 | 2 |
Limits
- The modulus must be prime and at most 1,000,000.
- The base is limited to plus or minus 10 to the 12th power.
- Primality is checked by trial division, so very large moduli are rejected.
- If p divides a, the main result is 0 rather than 1.
Common mistakes
- Using a composite modulus and expecting the theorem to hold.
- Forgetting the condition that p must not divide a.
- Building the full power before reducing modulo p.
- Reading a failed congruence as proof of primality.
- Confusing this theorem with Euler's theorem for composite moduli.
Try it yourself
Quick checks. Answers sit tucked under each question.
Base 2, prime 13. Main result?
1, since 2 to the 12th power is congruent to 1 modulo 13.
What is a to the power p modulo p?
It equals a modulo p, so base 3 with p = 7 gives 3.
What if a is divisible by p?
Both powers reduce to 0, and the theorem's nonzero condition does not apply.
Questions
What does the theorem say?
For prime p, a to the power p minus 1 is congruent to 1 modulo p when p does not divide a.
Why is it useful?
It simplifies large modular powers and helps compute modular inverses.
Does it test primality?
A failure proves compositeness, but a pass is not a guarantee because Carmichael numbers exist.
How is the modular power computed?
By repeated squaring with a reduction after every multiplication.
How is this related to Euler's theorem?
It is the special case of Euler's theorem for a prime modulus, where the totient is p minus 1.
What if the base is negative?
It is reduced into the residue range before the power is taken.
Sources
- University of Connecticut, Keith Conrad notes on Fermat's little theorem
- Stanford University crypto notes on the order of a unit
- Gordon College, Wilson's theorem and Fermat's little theorem