Fermat's Little Theorem Calculator
Given a prime p and integer a, verify Fermat's Little Theorem (a^(p-1) ≡ 1 mod p) and compute a^k mod p using fast modular exponentiation.
Inputs
Must be a prime number.
Any integer not divisible by p.
Computes a^k mod p for any k.
Fermat's Little Theorem Verification
Enter p and a, then click Calculate.
Modulus p
p is prime?
Base a
gcd(a, p)
Exponent p-1
a^(p-1) mod p
Custom Exponent Result
Exponent k
a^k mod p
Square-and-Multiply Steps
| Step | Bit | Squared | Result |
|---|
(Showing first 20 steps — full computation runs internally.)
Summary
Given a prime p and integer a, verify Fermat's Little Theorem (a^(p-1) ≡ 1 mod p) and compute a^k mod p using fast modular exponentiation.
How it works
- Enter a prime number p (the modulus).
- Enter an integer a (the base); a must not be divisible by p.
- The tool checks whether p is prime using a Miller-Rabin primality test.
- It computes a^(p-1) mod p and confirms the result equals 1 as the theorem predicts.
- Optionally, enter any exponent k to compute a^k mod p using the square-and-multiply algorithm.
- All arithmetic is performed with JavaScript BigInt for exact results on large numbers.
Use cases
- Verify primality concepts in number theory coursework.
- Check modular exponentiation results by hand for small primes.
- Explore the theorem with large primes to understand its scope.
- Test inputs for RSA and Diffie-Hellman key-exchange problems.
- Understand why a^(p-1) always returns 1 for prime moduli.
- Compute a^k mod p quickly without writing custom code.
Frequently Asked Questions
Last updated: 2026-06-10 ·
Reviewed by Nham Vu