Skip to main content

Fermats Little Theorem Calculator

Free Fermats little theorem Calculator for arithmetic. Enter values to get step-by-step solutions with formulas and graphs.

Share this calculator

Formula

a^(p-1) mod p = 1 (when p is prime and gcd(a,p) = 1)

Where a is any integer, p is a prime number, and gcd(a,p) = 1 means a is not divisible by p. The general form states a^p mod p = a mod p for any integer a. This theorem is the foundation of modular arithmetic and RSA cryptography.

Worked Examples

Example 1: Verifying Fermat's Little Theorem

Problem: Verify that 3^6 mod 7 = 1 using Fermat's Little Theorem (p=7, a=3).

Solution: Fermat's Little Theorem: a^(p-1) mod p = 1 when p is prime and gcd(a,p) = 1\nHere: a = 3, p = 7, p-1 = 6\ngcd(3, 7) = 1 (coprime, so theorem applies)\n\n3^6 = 3^2 * 3^2 * 3^2 = 9 * 9 * 9\n9 mod 7 = 2, so 3^6 mod 7 = 2 * 2 * 2 mod 7 = 8 mod 7 = 1\n\nThe theorem holds: 3^6 mod 7 = 1

Result: 3^6 mod 7 = 1 (verified)

Example 2: Finding Modular Inverse Using Fermat

Problem: Find the modular inverse of 5 modulo 13 using Fermat's Little Theorem.

Solution: By Fermat: a^(p-2) mod p is the inverse of a mod p\na = 5, p = 13, so inverse = 5^(13-2) mod 13 = 5^11 mod 13\n\nUsing repeated squaring:\n5^2 = 25 mod 13 = 12\n5^4 = 12^2 = 144 mod 13 = 1\n5^8 = 1^2 = 1\n5^11 = 5^8 * 5^2 * 5^1 = 1 * 12 * 5 = 60 mod 13 = 8\n\nVerification: 5 * 8 = 40 mod 13 = 1

Result: Inverse of 5 mod 13 = 8 (since 5 * 8 = 40 mod 13 = 1)

Frequently Asked Questions

What is Fermat's Little Theorem and what does it state?

Fermat's Little Theorem is a fundamental result in number theory discovered 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 a raised to the power (p-1) is congruent to 1 modulo p. In notation: a^(p-1) mod p = 1. There is also a more general form: for any integer a (even those divisible by p), a^p is congruent to a modulo p. For example, with a = 3 and p = 7: 3^6 = 729, and 729 mod 7 = 1. This theorem has profound implications for primality testing, cryptography, and computing modular inverses. It is one of the cornerstones of modern number theory.

How is Fermat's Little Theorem used in cryptography?

Fermat's Little Theorem is a key building block for the RSA encryption algorithm, one of the most widely used public-key cryptosystems. RSA relies on Euler's theorem, which generalizes Fermat's Little Theorem to composite moduli. In RSA, two large primes p and q are multiplied to get n = p * q, and encryption/decryption uses modular exponentiation where the theorem guarantees that the original message can be recovered. Specifically, the decryption exponent d is chosen so that e * d is congruent to 1 modulo the totient of n, and Fermat's theorem ensures that raising a ciphertext to the power d recovers the plaintext. Without this theorem, the mathematical foundation of RSA would not work.

What is the difference between Fermat's Little Theorem and Fermat's Last Theorem?

Despite the similar names, these are completely different theorems. Fermat's Little Theorem (1640) is about modular arithmetic: a^(p-1) mod p = 1 for prime p when gcd(a,p) = 1. It was proven by Euler in 1736 and is widely used in practical applications. Fermat's Last Theorem (1637) states that there are no positive integer solutions to a^n + b^n = c^n for any integer n greater than 2. It remained unproven for 358 years and was finally proved by Andrew Wiles in 1995 using extremely advanced mathematics involving elliptic curves and modular forms. The Little Theorem is elementary and has a simple proof, while the Last Theorem required decades of work by some of the greatest mathematicians.

How can Fermat's Little Theorem be used for primality testing?

Fermat's Little Theorem provides a necessary condition for primality: if p is prime, then a^(p-1) mod p = 1 for any a coprime to p. The contrapositive gives a primality test: if a^(p-1) mod p is not equal to 1 for some a, then p is definitely not prime. This is called the Fermat primality test. To test whether n is prime, choose random bases a and compute a^(n-1) mod n. If the result is not 1, n is composite. If it equals 1 for many bases, n is likely prime. However, certain composite numbers called Carmichael numbers (like 561) pass the Fermat test for all coprime bases. More robust tests like Miller-Rabin extend Fermat's approach to handle these cases.

How do you compute modular inverse using Fermat's Little Theorem?

If p is prime and a is not divisible by p, then the modular inverse of a modulo p is a^(p-2) mod p. This follows directly from the theorem: since a^(p-1) mod p = 1, we can write a * a^(p-2) mod p = 1, which means a^(p-2) is the multiplicative inverse of a modulo p. For example, to find the inverse of 3 mod 7: compute 3^(7-2) = 3^5 = 243, and 243 mod 7 = 5. Verification: 3 * 5 = 15, and 15 mod 7 = 1. This method is computationally efficient using fast modular exponentiation (repeated squaring), which runs in O(log n) multiplications. It is simpler to implement than the extended Euclidean algorithm for prime moduli.

What is Euler's theorem and how does it generalize Fermat's Little Theorem?

Euler's theorem states that if a and n are coprime (gcd(a,n) = 1), then a raised to the power of Euler's totient function of n is congruent to 1 modulo n. In notation: a^(phi(n)) mod n = 1, where phi(n) counts the integers from 1 to n that are coprime to n. When n = p (a prime), phi(p) = p - 1, and Euler's theorem reduces exactly to Fermat's Little Theorem. For composite n, the totient is smaller: phi(12) = 4 (the numbers 1, 5, 7, 11 are coprime to 12). So 5^4 mod 12 = 625 mod 12 = 1. Euler's theorem is the foundation of RSA encryption, where the modulus n = p*q and phi(n) = (p-1)(q-1).

References