Skip to content

Fermat’s Little Theorem Calculator

Updated:

Verify Fermat's Little Theorem online: enter a base a and a prime p, and the calculator computes a^(p-1) mod p and checks whether the result equals 1. Useful for number theory, primality testing and cryptography.

Check Fermat’s Theorem or Compute Modulo Powers

Input Fields
a
Enter an integer a where 1 ≤ a < p
p
Enter a prime number p
If enabled, the result will update automatically when you change any value.

Fermat's Little Theorem Formula

Formula
\[\text{If } p \text{ is prime and } a \not\equiv 0 \pmod{p}, \text{ then } a^{p-1} \equiv 1 \pmod{p}\]

Where:

  • \(p\) — a prime number (the modulus)
  • \(a\) — an integer with \(1 \le a < p\), i.e. not divisible by \(p\)
  • \(a^{p-1} \bmod p\) — the remainder of \(a^{p-1}\) upon division by \(p\)

The theorem states that for any prime \(p\) and any integer \(a\) not divisible by \(p\), raising \(a\) to the power \(p-1\) and reducing modulo \(p\) always leaves a remainder of 1. An equivalent form, \(a^p \equiv a \pmod{p}\), holds for all integers \(a\), including multiples of \(p\).

Fermat's Little Theorem – Calculation Example

Example: take the default values \(a = 5\) and \(p = 7\). Since 7 is prime and 5 is not divisible by 7, the theorem should apply.

\[a^{p-1} \bmod p = 5^{7-1} \bmod 7 = 5^6 \bmod 7\]

\[5^6 = 15625, \qquad 15625 = 7 \times 2232 + 1 \;\Rightarrow\; 15625 \bmod 7 = 1\]

Result: \(5^6 \equiv 1 \pmod{7}\), so the theorem holds. ✅

How to use the calculator

  1. Enter the base \(a\) in the Base (a) field. It must satisfy \(1 \le a < p\).
  2. Enter a prime number in the Prime modulus (p) field (e.g. 2, 3, 5, 7, 11…).
  3. Read the Result of a^(p−1) mod p — the remainder of \(a^{p-1}\) divided by \(p\).
  4. Check Theorem holds? — it shows "Yes" when the remainder equals 1, which is the case for every valid input.

If you enter a non-prime modulus, a base outside the allowed range, or \(a \ge p\), the calculator shows an error message instead of a result.

What Fermat's Little Theorem means

Fermat's Little Theorem, stated by Pierre de Fermat in 1640, is one of the cornerstones of elementary number theory. It says that prime numbers impose a remarkably regular structure on powers: no matter which base \(a\) you choose (as long as \(p\) does not divide \(a\)), the power \(a^{p-1}\) always reduces to exactly 1 modulo \(p\). In other words, \(a^{p-1}\) is always a multiple of \(p\) plus one.

Why does this matter? First, it is a powerful computational shortcut. Instead of computing a huge power like \(3^{100}\) directly, you can reduce the exponent modulo \(p-1\) and work with much smaller numbers — a technique used constantly in modular arithmetic. Second, it underlies primality testing: if \(a^{p-1} \bmod p \ne 1\) for some \(a\), then \(p\) is definitely not prime. This is the idea behind Fermat's test and, in refined form, the Miller–Rabin test used to find large primes.

Third, the theorem is essential in cryptography. RSA encryption and decryption are essentially modular exponentiations, and Fermat's Little Theorem (together with its generalization, Euler's Theorem) is what guarantees that decrypting a message recovers the original. It also leads directly to the formula for finding multiplicative inverses modulo a prime: since \(a^{p-1} \equiv 1 \pmod{p}\), the inverse of \(a\) is simply \(a^{p-2} \bmod p\).

How to interpret the result

For any valid input — a prime \(p\) and a base \(a\) with \(1 \le a < p\) — the calculator will always return 1 and "Yes". That is precisely the content of the theorem: it is a statement about all such pairs, not just some. If you want to see why it fails for non-primes, try \(a = 2\), \(p = 9\): \(2^8 = 256\) and \(256 \bmod 9 = 4 \ne 1\). The theorem is a one-way test — a non-prime always fails for at least one base, while a pass does not by itself prove primality (see the note on Carmichael numbers below).

Assumptions, limitations and tips

  • The modulus must be prime. The theorem does not hold for composite moduli in general. For composite \(n\), use Euler's Theorem with \(a^{\varphi(n)} \equiv 1 \pmod{n}\) instead, where \(\varphi\) is Euler's totient function and \(\gcd(a, n) = 1\).
  • The base must not be divisible by \(p\). If \(a \equiv 0 \pmod{p}\), then \(a^{p-1} \equiv 0\), not 1. The equivalent form \(a^p \equiv a \pmod{p}\) still holds in that case.
  • Fermat tests can be fooled. There exist composite numbers called Carmichael numbers (the smallest is 561) that satisfy \(a^{n-1} \equiv 1 \pmod{n}\) for every \(a\) coprime to \(n\). A single passing test is therefore not proof of primality.
  • Work with reduced exponents. In hand calculations, combine the theorem with successive squaring: reduce exponents modulo \(p-1\) and intermediate products modulo \(p\) to keep numbers small.
  • Related tools. For systems of congruences, see the Chinese Remainder Theorem calculator; for division in modular arithmetic, the multiplicative inverse modulo calculator is a natural companion.

Frequently Asked Questions

What is Fermat's Last Theorem — is it the same thing?

No, they are different results often confused because of their names. Fermat's Little Theorem (1640) is about modular exponentiation: a^(p-1) ≡ 1 (mod p) for prime p. Fermat's Last Theorem states that no positive integers x, y, z satisfy x^n + y^n = z^n for n > 2; it was conjectured by Fermat but only proved by Andrew Wiles in 1995.

Leave a Reply

Your email address will not be published. Required fields are marked *