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.

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

  1. Enter a prime number p (the modulus).
  2. Enter an integer a (the base); a must not be divisible by p.
  3. The tool checks whether p is prime using a Miller-Rabin primality test.
  4. It computes a^(p-1) mod p and confirms the result equals 1 as the theorem predicts.
  5. Optionally, enter any exponent k to compute a^k mod p using the square-and-multiply algorithm.
  6. 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