Fermat's Little Theorem Calculator

Verify standard and alternative forms of Fermat's Little Theorem with fast modular exponentiation.

Calculate a Fermat congruence
Enter an integer base and a prime modulus, then choose the theorem form.

About Fermat's Little Theorem

Fermat's Little Theorem connects prime numbers, powers, and modular arithmetic. Its standard form says that if p is prime and a is not divisible by p, then a^(p - 1) leaves remainder 1 when divided by p. In congruence notation, a^(p - 1) is congruent to 1 modulo p. For a = 2 and p = 7, the calculation is 2^6 = 64, and 64 leaves remainder 1 after division by 7. An equivalent form states that a^p is congruent to a modulo p for every integer a when p is prime. This version also covers the case where p divides a, because both sides then have remainder zero. The standard form needs the greatest common divisor of a and p to equal 1. The calculator checks primality and this coprime condition before evaluating the selected congruence. Directly calculating a large power can produce an enormous intermediate number. Instead, the tool uses binary modular exponentiation, also called repeated squaring. It repeatedly squares the current base while reducing modulo p after every multiplication. This preserves the exact remainder, avoids unwieldy full powers, and needs only a logarithmic number of exponent steps. The method is fundamental in practical cryptographic software. The theorem can prove that a number is composite: if a coprime base fails the congruence, the proposed modulus cannot be prime. Passing one Fermat test, however, does not prove primality. Some composites pass for particular bases, and Carmichael numbers pass the standard test for every base coprime to them. Reliable primality testing therefore uses stronger deterministic checks or tests such as Miller-Rabin with carefully selected bases. Fermat's result supports many areas of number theory and computing. It helps reduce exponents in modular calculations, motivates primality tests, and contributes to the mathematical foundations of public-key cryptography. RSA relies more directly on Euler's related theorem, but Fermat's theorem explains the prime-modulus behavior at its core. Modular inverses can also be found with a^(p - 2) modulo p when p is prime and a is nonzero modulo p. Use this calculator to verify classroom examples and explore modular patterns, not to certify large cryptographic primes. Inputs are limited to JavaScript safe integers for reliable primality checking, while the exponentiation itself uses exact integer arithmetic. A successful result confirms that the chosen prime and base satisfy the selected theorem form; it does not by itself establish that an unchecked composite candidate is prime.

Fermat's Little Theorem examples

Each example reduces a power without constructing the full value.

Inputs and formRemainderInterpretation
a = 2, p = 7, standard2^6 mod 7 = 1The standard congruence is verified.
a = 3, p = 11, standard3^10 mod 11 = 1A classic prime-modulus example.
a = 5, p = 13, alternative5^13 mod 13 = 5The alternative form returns the base remainder.
a = 17, p = 17, alternative17^17 mod 17 = 0The alternative form remains valid when p divides a.

How to use the theorem calculator

  1. Enter an integer greater than 1 as the base a.
  2. Enter a prime integer as the modulus p.
  3. Choose the standard form for coprime inputs or the alternative form for any integer base.
  4. Select Calculate Theorem to evaluate the power with modular exponentiation.
  5. Review the remainder and verification statement.

Fermat's Little Theorem FAQ

What does modulo p mean?

Modulo p means that numbers are compared by their remainders after division by p. Two numbers are congruent modulo p when those remainders match.

Why must p be prime?

Primality is a required hypothesis of Fermat's Little Theorem. Composite moduli do not consistently satisfy the congruence, although some can pass selected tests.

What is the difference between the two theorem forms?

The standard form returns 1 and requires a to be coprime to p. The alternative form returns the same remainder as a and works for every integer a when p is prime.

Does passing a Fermat test prove a number is prime?

No, some composite numbers pass Fermat tests for certain bases. A failed test proves compositeness, but a passed test requires stronger primality checks for certainty.

How does fast modular exponentiation work?

It decomposes the exponent into binary powers and repeatedly squares the base. Reducing after every multiplication keeps values small while preserving the exact remainder.