Fermats Little Theorem Calculator

Fermat's Little Theorem

Verify a^(p-1) ≡ 1 (mod p) for prime p and integer a. Fundamental to modular arithmetic and cryptography.

Last updated: May 2026 | By Patchworkr Team

Result will appear here...

Reference Examples

apa^(p-1) mod p
251
371
271
5111
2131

What is Fermat's Little Theorem?

Fermat's Little Theorem states: if $p$ is a prime number and $a$ is an integer not divisible by $p$, then $a^6 \equiv 1 \pmod7$. This elegant result connects modular arithmetic with prime numbers and is foundational to number theory.

Named after French mathematician Pierre de Fermat, this theorem has extensive applications in cryptography (RSA encryption), primality testing, and computational mathematics. It's one of the most important results in elementary number theory and appears frequently in advanced algorithm design.

The Formula

a^(p-1) ≡ 1 (mod p)

Conditions: $p$ must be prime and $a$ must not be a multiple of $p$.

How to Use This Verifier

  1. Enter base `a` (any positive integer).
  2. Enter prime `p` (must be a prime number).
  3. Click Verify to compute `a^(p-1) mod p` using efficient modular exponentiation.
  4. For valid inputs, the result should display `1`.
  5. If you get a different result, verify that `p` is prime and `a` is not divisible by `p`.

Example: a = 3, p = 7

Check:
7 is prime ✓
Compute:
3^6 mod 7
Result:
729 mod 7 = 1 ✓

Frequently Asked Questions

Why is this theorem important in cryptography?

It's the mathematical foundation of RSA encryption and other cryptographic systems that rely on modular exponentiation properties.

What happens if p is not prime?

The theorem doesn't apply; for composite p, a^(p-1) mod p may not equal 1.

What if a is divisible by p?

Then a ≡ 0 (mod p), so a^(p-1) ≡ 0 (mod p), not 1. The theorem requires a to be coprime to p.

How is this used in primality testing?

If a^(n-1) ≢ 1 (mod n) for any a coprime to n, then n is definitely composite.

What's the connection to Euler's Theorem?

Euler's Theorem generalizes Fermat's Little Theorem: a^φ(n) ≡ 1 (mod n) for coprime a and n.

Can this theorem predict primes?

Not directly — the converse isn't always true (Carmichael numbers), but it helps eliminate composites efficiently.

How does modular exponentiation work?

It repeatedly squares and reduces mod p, computing a^(p-1) efficiently without huge intermediate values.

What are Carmichael numbers?

Composite numbers n where a^(n-1) ≡ 1 (mod n) for all a coprime to n, tricking simple primality tests.

Related Tools