Fermat's Little Theorem Calculator

Verify Fermat's little theorem, compute a^(p-1) mod p, perform modular exponentiation, check primality, view computation steps, and explore theorem properties with presets.

Should be prime for Fermat's theorem
Leave blank to use p−1
2^6 mod 7
1
Result is 1 — consistent with Fermat's theorem
p is Prime?
Yes ✓
7 is a prime number
GCD(a, p)
1
a and p are coprime — theorem applies
a mod p
2
2 reduced modulo 7
a^p mod p
2
2^7 mod 7 — should equal a mod p = 2 if p is prime
Theorem Holds?
Yes ✓
Fermat's little theorem is verified for these values

Verification Visual

a^(p−1) mod p
1
Expected: 1
a^p mod p
2
Expected: 2

Fermat Test for Bases 2–10

Base aa^(p−1) mod pGCD(a,p)Result
211Pass ✓
311Pass ✓
411Pass ✓
511Pass ✓
611Pass ✓
707Not coprime
811Pass ✓
911Pass ✓
1011Pass ✓
Properties & Related Theorems
PropertyStatement
Fermat's Little Theorema^(p−1) ≡ 1 (mod p) when p prime, gcd(a,p)=1
Alternate forma^p ≡ a (mod p) for all integers a
Euler's Theorema^φ(n) ≡ 1 (mod n) when gcd(a,n)=1
Modular Inversea^(p−2) mod p gives the modular inverse of a
Carmichael NumbersComposites where a^(n−1)≡1 (mod n) for all coprime a; e.g. 561, 1105, 1729
Miller-RabinProbabilistic test that extends Fermat; not fooled by Carmichael numbers
Wilson's Theorem(p−1)! ≡ −1 (mod p) if and only if p is prime
Planning notes, formulas, and examples

About the Fermat's Little Theorem Calculator

**Fermat's Little Theorem Calculator** lets you explore one of the cornerstones of number theory and modern cryptography. The theorem states that if p is a prime number and a is any integer not divisible by p, then a^(p−1) ≡ 1 (mod p). This elegant result, first stated by Pierre de Fermat in 1640, underpins primality testing algorithms and the RSA cryptosystem.

Enter a base value a and a modulus p, and this calculator computes a^(p−1) mod p using fast modular exponentiation (binary method). If the result is 1 and p is indeed prime, the theorem is verified. If the result is not 1, then p is definitely composite — giving you a quick (though not foolproof) primality test.

The tool also computes arbitrary modular exponentiations a^e mod m for any exponent e and modulus m you choose, not just the p−1 case. A step-by-step breakdown shows the binary expansion of the exponent and the squaring-and-multiplying process at each bit, making it an excellent learning resource for understanding how computers perform modular arithmetic efficiently.

Preset buttons load classic examples including small primes, Carmichael numbers (pseudoprimes that fool the Fermat test), and large values used in cryptographic contexts. A properties reference table lists key corollaries and related theorems, while the computation steps table shows every intermediate result. Whether you are a student learning abstract algebra, preparing for a competition, or studying cryptographic protocols, this calculator makes Fermat's little theorem concrete and interactive.

When This Page Helps

Modular exponentiation with large numbers is impractical by hand — computing 2^100 mod 101 requires either a clever shortcut or a computer. This calculator verifies Fermat's little theorem step by step, showing the intermediate squaring operations so you can follow the binary exponentiation method. It also tests whether a number passes the Fermat primality test and flags Carmichael numbers that fool the basic test. Cryptography students use it to understand the RSA underpinnings, while competitive programmers use it to check modular inverse computations.

How to Use the Inputs

  1. Enter a base value a and a prime modulus p.
  2. Optionally enter a custom exponent (defaults to p−1).
  3. Click a preset button to load a classic example.
  4. Review output cards for the result, primality verdict, and GCD.
  5. Examine the computation steps table for the binary exponentiation process.
  6. Check the properties reference for related theorems and corollaries.
Formula used
If p is prime and gcd(a, p) = 1, then a^(p−1) ≡ 1 (mod p). Equivalently, a^p ≡ a (mod p) for all a.

Example Calculation

Result: 2

Let a = 2, p = 7. Then 2^6 mod 7 = 64 mod 7 = 1 ✓. Since the result is 1 and 7 is prime, Fermat's theorem holds.

Tips & Best Practices

  • Check that all inputs use the same scale and assumptions before trusting the result.
  • Compare the answer with the worked example or a rough estimate to catch entry mistakes.

How the Theorem Works

Fermat's little theorem states that if p is prime and a is not divisible by p, then a^(p−1) ≡ 1 (mod p). The proof relies on the fact that multiplying the set {1, 2, …, p−1} by a (mod p) simply permutes the set, so the products are equal — leading to a^(p−1) ≡ 1 after cancellation. This elegant argument generalises to Euler's theorem using the totient function.

Primality Testing and Carmichael Numbers

The Fermat test works by checking whether a^(n−1) ≡ 1 (mod n) for random bases a. If it fails, n is definitely composite. However, Carmichael numbers (561, 1105, 1729, …) pass the test for every coprime base, making the basic Fermat test probabilistic rather than deterministic. The Miller–Rabin test refines this by also checking square roots of 1, eliminating most Carmichael-number false positives.

Applications in Cryptography

RSA encryption chooses e and d such that ed ≡ 1 (mod φ(n)), ensuring that (m^e)^d ≡ m (mod n) by Euler's theorem. Fermat's little theorem is the special case that makes textbook RSA examples tractable: choosing small primes p and q lets students verify the entire encrypt–decrypt cycle by hand. Modular exponentiation via repeated squaring — the algorithm this calculator demonstrates — is the same routine used in production RSA implementations.

Sources & Methodology

Last updated:

Frequently Asked Questions

  • If p is prime and a is not divisible by p, then a^(p−1) ≡ 1 (mod p). It connects prime numbers to modular arithmetic.