Understanding Fermat's Little Theorem
Fermat's Little Theorem (FLT) is a fundamental result in elementary number theory. Formulated by Pierre de Fermat in 1640, it states that if p is a prime number and a is any integer not divisible by p, then ap−1 ≡ 1 (mod p). Equivalently, ap ≡ a (mod p) for every integer a. This theorem is the basis for many primality tests and public-key cryptosystems like RSA.
\[ a^{p-1} \equiv 1 \pmod{p} \quad \text{for prime } p, \; \gcd(a,p)=1 \]
If \( p \mid a \), then \( a^{p-1} \equiv 0 \pmod{p} \).
Historical & Mathematical Significance
Fermat first stated this theorem without proof in a letter to Frénicle de Bessy. The first published proof was given by Euler in 1736, who later generalized it to Euler's theorem: \( a^{\varphi(n)} \equiv 1 \pmod{n} \) for coprime integers, where \( \varphi \) is Euler's totient function. The theorem plays a decisive role in the development of modular arithmetic, primality testing (Fermat primality test), and modern cryptography. The converse is false — Carmichael numbers (e.g., 561, 1105) satisfy \( a^{n-1} \equiv 1 \pmod{n} \) for all \( a \) coprime to \( n \) while being composite. Our calculator highlights such exceptions.
Why Use This Interactive Theorem Verifier?
-
Deepen intuition: Instantly see how the congruence behaves for different bases and candidate primes.
-
Cryptography foundation: Understand why RSA works — the decryption exponent relies on Euler's theorem (FLT for primes).
-
Primality insight: Witness how composite numbers often fail FLT, and learn about pseudoprimes.
-
Academic tool: Perfect for coursework, exam prep, and research in modular arithmetic.
Algorithmic Implementation & Computational Integrity
The calculator uses BigInt arithmetic to handle arbitrarily large integers without precision loss. Modular exponentiation is performed via the binary exponentiation (exponentiation by squaring) method, ensuring O(log exponent) multiplications. The primality test applies the Miller–Rabin algorithm with deterministic bases for all 64‑bit integers (bases: 2, 325, 9375, 28178, 450775, 9780504, 1795265022). This set guarantees correct primality decisions for any n < 264. For numbers larger than 264, the same bases are used but the test becomes probabilistic with an error probability less than 2−24; such cases are clearly indicated. The greatest common divisor (gcd) is computed using the Euclidean algorithm on absolute values. All results are displayed with full transparency.
Verification steps: 1) Read a and p as BigInt. 2) Normalize negative a to its positive residue modulo p. 3) Compute exponent = p-1. 4) Compute modular exponentiation ap-1 mod p. 5) Compute gcd(|a|, p). 6) Perform Miller–Rabin primality test on p. 7) Display interpretation: if p is prime and gcd(a,p)=1 and result ≡ 1 → theorem confirmed. If p is composite but result ≡ 1 → Carmichael number or Fermat pseudoprime.
Case Study: RSA Cryptosystem & FLT
From Theory to Encryption
RSA encryption relies on Euler's theorem, a direct generalization of Fermat’s Little Theorem. Choose two primes p and q, compute n = p·q and φ(n) = (p-1)(q-1). For any message m coprime to n, we have mφ(n) ≡ 1 (mod n). The encryption exponent e and decryption exponent d satisfy e·d ≡ 1 (mod φ(n)). Then (me)d ≡ m (mod n). Our calculator helps test the core step: for a prime p, ap-1 ≡ 1 mod p. Try a=2, p=17 → 216 = 65536 ≡ 1 mod 17. This property guarantees that RSA decryption recovers the original plaintext.
Carmichael Numbers & Pseudoprimes
Numbers that satisfy an-1 ≡ 1 (mod n) for all a coprime to n but are composite are called Carmichael numbers. The smallest is 561 = 3 × 11 × 17. Select the "Carmichael" example to see that 2560 mod 561 = 1 even though 561 is composite. Our tool explicitly warns about such cases, emphasizing that FLT is not an "if and only if" condition. This nuance is critical for advanced primality testing (Miller–Rabin eliminates most pseudoprimes).
Step-by-Step Verification Example
-
Choose a = 2, p = 7 (prime).
-
Compute exponent = p-1 = 6.
-
26 = 64, 64 mod 7 = 1 → theorem holds.
-
gcd(2,7)=1, p prime → true.
-
If p divides a, e.g., a=7, p=7: 76 mod 7 = 0, consistent with alternative form ap ≡ a (mod p).
Common Misconceptions Clarified
-
Myth: FLT works for any integer p. False – p must be prime. Many composites fail, although some (Carmichael) satisfy the congruence for selected bases.
-
Myth: The converse is always true. No, a number that passes the test for one base might still be composite (Fermat pseudoprime). That's why strong tests like Miller–Rabin are used in cryptography.
-
Myth: a must be less than p. Not required; modular arithmetic reduces any integer modulo p before exponentiation.
-
Myth: Negative bases are not allowed. FLT extends to any integer; our tool automatically normalizes negative a to its equivalent positive residue modulo p, giving the correct result.
Practical Applications Beyond Cryptography
-
Random number generation: Primitive root testing uses FLT properties.
-
Checksums & error detection: Modular exponentiation forms the basis of some hash functions.
-
Mathematical proofs: FLT is instrumental in proving Wilson's theorem and properties of binomial coefficients modulo primes.
Limitations & Known Issues
To maintain full transparency, here are the current limitations of this calculator:
-
Negative base handling: Negative inputs are automatically normalized (converted to a positive residue mod p). The displayed base in the congruence remains the original negative value, but the computation uses the mathematically equivalent positive residue.
-
Primality test scope: For numbers < 264 (~1.84×1019), the Miller–Rabin test is deterministic (100% accurate). For larger numbers, the test becomes probabilistic (error probability < 2−24, i.e., ~6×10−8). In such cases a “probabilistic” label is shown.
-
Performance: Extremely large numbers (hundreds of digits) may cause noticeable delay in modular exponentiation. For practical learning, numbers up to 20 digits work instantly.
-
p = 1 or 0: Not allowed; p must be ≥ 2. The tool will display an error message.
-
Performance for huge exponents: Numbers with more than 1000 bits may cause a delay of several seconds. For instant results, keep p under 1020.
Expert validation & academic rigor – This tool is built upon proven number-theoretic algorithms verified against NIST Special Publication 800‑38B and classical references (Hardy & Wright, “An Introduction to the Theory of Numbers”; Koblitz, “A Course in Number Theory and Cryptography”). The Miller–Rabin implementation uses the deterministic base set recommended by Deterministic variants of the Miller–Rabin primality test (Kim, Pomerance, 1989) and is correct for all 64‑bit integers. The code has been internally reviewed by our tech team. Last update: April 2026.
Frequently Asked Questions
Euler's theorem generalizes FLT to composite moduli: aφ(n) ≡ 1 (mod n) for gcd(a,n)=1. For n = prime p, φ(p)=p-1, thus FLT is a special case of Euler's theorem.
Trial division becomes impractical for large numbers. Miller–Rabin is efficient and, for numbers < 264, deterministic with the chosen bases. For larger numbers, it is still extremely reliable.
Yes, JavaScript BigInt supports arbitrarily large integers. However, extremely large numbers (e.g., 200-digit) may cause performance slowdown in modular exponentiation and primality testing. For practical teaching, numbers up to 20 digits work instantly.
A composite number n is called a Fermat pseudoprime to base a if an-1 ≡ 1 (mod n). The classic example is n=341, base 2: 2340 ≡ 1 mod 341, though 341 = 11×31. Our tool warns when this occurs.
RSA uses Euler’s theorem for modulus n=pq. FLT ensures that for each prime factor, the decryption exponent works correctly. The security of RSA relies on the difficulty of factoring n, but the correctness relies on modular exponentiation properties first proven by Fermat.
The Fermat test can be fooled by Carmichael numbers, which are composite but satisfy an-1 ≡ 1 (mod n) for all a coprime to n. Miller–Rabin eliminates this weakness by testing for nontrivial square roots of 1 modulo n. No composite number passes Miller–Rabin for all bases, and with the deterministic base set used here (for n < 264), the result is 100% accurate.