Advanced

Fermat's Little Theorem Calculator

Enter a base a and a candidate prime p to compute a^(p−1) mod p. If the result is 1, p passes Fermat's test for this witness; the calculator also confirms whether p is truly prime and derives the modular inverse a⁻¹ (mod p) = a^(p−2) mod p when p is prime.
Any integer; can be negative
Candidate prime p ≥ 2 (try a known prime such as 13, 97, 101)
a^(p−1) mod p
1

Result is 1 — theorem confirmed: p is prime and gcd(a, p) = 1

a^(p−1) mod p
1
p is prime
Yes
gcd(a, p)
1
a and p coprime
Yes
Fermat test passes
Yes
Modular inverse a⁻¹ (mod p)
2
Fermat's little theorem — step by step
1

Fermat's theorem: if p is prime and gcd(a, p) = 1:

a^(p−1) ≡ 1 (mod p)
2

Substitute values:

7^(13 − 1) mod 13 = 7^12 mod 13
=

Result — theorem holds:

= 1 ✓
01.63.34.96.58.19.811.413Result on the number line mod p (lands on 1 if p is prime and gcd(a,p)=1)
Step by step
  1. 1

    Exponent e = p − 1

    e = 13 − 1 = 12
  2. 2

    gcd(a, p)

    gcd(7, 13) = 1
  3. 3

    aᵉ mod p

    7^12 mod 13 = 1
Results are estimates for general information only and are not professional advice — always verify important results independently before relying on them. Read the full disclaimer.
Quick answer

How does this calculator work?

Fermat's little theorem: for prime p and gcd(a, p) = 1, a^(p−1) ≡ 1 (mod p). Compute with binary exponentiation using BigInt. If result ≠ 1, p is definitely composite. If result = 1, p passes the Fermat test — but Carmichael numbers can fool it, so the calculator adds a deterministic Miller–Rabin check. When p is prime, a^(p−2) mod p gives the modular inverse of a.

Formula
Fermat's little theorem: a^(p−1) ≡ 1 (mod p) when p is prime and gcd(a, p) = 1 • Modular inverse: a⁻¹ ≡ a^(p−2) (mod p)
How this is calculated

Fermat's little theorem states that if p is a prime and a is any integer not divisible by p, then a^(p−1) is congruent to 1 modulo p. Equivalently, a^p ≡ a (mod p) for any integer a. The theorem is one of the fundamental results of modular arithmetic and underlies the RSA cryptosystem, Miller–Rabin primality testing, and fast computation of modular inverses.

This calculator uses binary exponentiation (exponentiation by squaring) with JavaScript BigInt to compute a^(p−1) mod p exactly, without overflow, for any p up to 10^15. Primality is checked with a deterministic variant of the Miller–Rabin test using the witness set {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}, which gives correct results for all integers below approximately 3.3 × 10^24 — far larger than the input limit.

When p is prime and gcd(a, p) = 1, the modular inverse of a mod p is given by a^(p−2) mod p — a direct consequence of the theorem, since a × a^(p−2) ≡ a^(p−1) ≡ 1 (mod p). One critical caveat: a^(p−1) ≡ 1 (mod p) for a composite p does NOT prove primality. Composite numbers that pass Fermat's test are called Fermat pseudoprimes; those that pass for every base coprime to them are Carmichael numbers (e.g., 561, 1105, 1729). The deterministic Miller–Rabin check used here correctly identifies all Carmichael numbers as composite.

Frequently asked questions

It means p is definitively not prime — the base a is a Fermat witness proving compositeness. Any single witness that produces a result other than 1 is sufficient proof that p is composite.

Yes. Composite numbers called Fermat pseudoprimes pass the test for particular bases. Carmichael numbers — such as 561, 1105 and 1729 — pass for every base coprime to them. The Miller–Rabin primality check used here is immune to this weakness.

Fermat's theorem gives a^(p−1) ≡ 1 (mod p), which can be rewritten as a × a^(p−2) ≡ 1 (mod p). That is exactly the definition of a multiplicative inverse modulo p. This trick only works when p is prime; for composite moduli, use the extended Euclidean algorithm instead.

Also known as

fermat little theorem calculator
a to the p minus 1 mod p
fermat primality test calculator
modular inverse fermat method
fermat pseudoprime checker
number theory modular arithmetic
fermat witness composite test

APA

TG we-Calculate Editorial Team. (2026). Fermat's Little Theorem Calculator [Online calculator]. TG we-Calculate. https://we-calculate.com/calculator/fermats-little-theorem-calculator

Chicago

TG we-Calculate Editorial Team. "Fermat's Little Theorem Calculator." TG we-Calculate. 2026. https://we-calculate.com/calculator/fermats-little-theorem-calculator.

IEEE

TG we-Calculate Editorial Team, "Fermat's Little Theorem Calculator," TG we-Calculate, 2026. [Online]. Available: https://we-calculate.com/calculator/fermats-little-theorem-calculator

BibTeX

@misc{wecalculate_fermats_little_theorem_calculator, title = {Fermat's Little Theorem Calculator}, author = {{TG we-Calculate Editorial Team}}, howpublished = {\url{https://we-calculate.com/calculator/fermats-little-theorem-calculator}}, year = {2026}, note = {TG we-Calculate} }

Did this calculator help you?