Last verified · v1.0
Calculator · math
Fermat's Little Theorem Calculator
Calculate Fermat's Little Theorem results: verify a^(p-1) ≡ 1 (mod p), compute modular exponents, and find modular inverses for any prime modulus.
Inputs
Result (a^n mod p)
—
Explain my result
Get a plain-English breakdown of your result with practical next steps.
The formula
How the
result is
computed.
What Is Fermat's Little Theorem?
Fermat's Little Theorem, first stated by Pierre de Fermat in 1640 and formally proved by Leonhard Euler in 1736, is one of the most important results in elementary number theory. The theorem establishes a precise congruence relationship between integers and prime numbers, forming the mathematical backbone of RSA encryption, digital signatures, and probabilistic primality testing used in modern computer security.
The Core Formula
The theorem takes two equivalent forms. For any integer a and prime p:
- General form: ap ≡ a (mod p) — valid for all integers a
- Reduced form: ap−1 ≡ 1 (mod p) — valid when gcd(a, p) = 1
The reduced form applies whenever a is not a multiple of p. Under that condition, a and p share no common factor greater than 1, which guarantees that a has a well-defined multiplicative inverse modulo p.
Variable Reference
- Base (a): Any integer. In cryptographic contexts, a commonly represents a plaintext message, a key component, or a randomly chosen witness for primality testing.
- Prime Modulus (p): A prime number serving as the modulus. Primality is a strict requirement — the theorem does not hold for arbitrary composite moduli without invoking Euler's generalization.
- Exponent (n): The power to which a is raised when computing an mod p directly. Fermat's theorem permits replacing n with n mod (p−1), dramatically reducing the size of the exponent before calculation.
Step-by-Step Calculation Method
- Confirm that p is prime using a reliable primality test such as trial division for small values or Miller-Rabin for large values.
- Verify that gcd(a, p) = 1 — that is, a is not divisible by p.
- To compute an mod p efficiently, reduce the exponent: compute r = n mod (p−1).
- Evaluate ar mod p using repeated squaring (binary exponentiation), which completes in O(log r) multiplications.
Worked Examples
Example 1 — Verifying the Reduced Form
Let a = 3, p = 7. Since gcd(3, 7) = 1, the reduced form applies. Compute 36 = 729. Dividing: 729 = 104 × 7 + 1, so 36 ≡ 1 (mod 7). The theorem holds.
Example 2 — Reducing a Large Exponent
Compute 2100 mod 7. With p = 7, reduce the exponent: r = 100 mod 6 = 4. Therefore 2100 ≡ 24 = 16 ≡ 2 (mod 7). This result is obtained without evaluating the 31-digit number 2100 directly.
Example 3 — Computing a Modular Inverse
The modular inverse of a modulo p equals ap−2 mod p. For a = 3 and p = 7: inverse = 35 mod 7 = 243 mod 7 = 5. Verification: 3 × 5 = 15 = 2 × 7 + 1, confirming 3 × 5 ≡ 1 (mod 7).
Real-World Applications
RSA Cryptography
RSA encryption exploits Fermat's theorem through Euler's generalization. During key generation, two primes p and q — each typically containing 150 or more decimal digits in modern 2048-bit implementations — are chosen. Public exponent e and private exponent d satisfy ed ≡ 1 (mod (p−1)(q−1)), so that encrypting a message m as me and decrypting as (me)d recovers m exactly.
Primality Testing
The Fermat primality test checks a candidate n by verifying an−1 ≡ 1 (mod n) for multiple random bases a. Failure for any single base proves n is composite. This principle underlies the Miller-Rabin algorithm, which powers primality testing in OpenSSL, Python's sympy library, and Java's BigInteger.isProbablePrime method.
Sources and Methodology
The calculations performed by this tool follow the congruence arithmetic framework established in Congruences and Congruence Equations (University of California, Irvine, Math 180A) and the algorithmic treatment of modular exponentiation in Lecture Notes on Fermat's Little Theorem (University of South Carolina, CS 2112). Theoretical foundations draw from Theory of Numbers (Kansas State University) and the worked problem sets in Carnegie Mellon University ARML Number Theory Solutions.
Reference