Mathematics calculator

Fermat’s Little Theorem Calculator

Evaluate a^(p−1) modulo p with exact modular arithmetic and check whether Fermat’s prime-modulus condition applies.

Calculator inputs

Enter your values

Modular arithmetic
Calculated output

Result summary

Your result will appear here

Complete the required fields and select the calculate button.

What is Fermat's Little Theorem?

Fermat’s little theorem is a shortcut for a particular kind of remainder problem. When p is prime, raising an integer a to the power p − 1 produces a remainder of 1 after division by p, provided a is not itself divisible by p. The calculator evaluates that modular power directly, so it can also show the divisible case, where the remainder is 0.

This result is useful in elementary number theory, modular arithmetic, primality discussions and public-key cryptography. It handles powers without first writing out an enormous ordinary integer. That distinction matters: modular arithmetic is concerned with the remainder, not the full decimal expansion of the power.

How to Use This Calculator

  1. Enter an integer for a. Positive, zero and negative integers are allowed.
  2. Enter a prime number for p. The page checks this condition because the named theorem requires a prime modulus.
  3. Select Calculate. The result line shows ap−1 and its remainder modulo p.

The calculation uses repeated squaring. It reduces each intermediate multiplication modulo p, rather than evaluating the entire power and then dividing. For example, a power with hundreds of millions of factors can still be reduced through a short sequence of squaring steps. This also avoids the rounding that occurs when very large integers are stored as ordinary decimal approximations.

If a and p have no common factor and p is prime, expect a remainder of 1. If a is a multiple of p, every positive power remains divisible by p, so the displayed remainder is 0. A composite value of p is rejected because a result for that input would not be an application of Fermat’s little theorem.

Formula Used

For a prime p and an integer a with p not dividing a:

ap−1 ≡ 1 (mod p)

An equivalent form is ap ≡ a (mod p), which works for every integer a. The symbol ≡ means that the two sides leave the same remainder when divided by p. The calculator reports ap−1 mod p, so the non-divisibility condition explains whether the expected result is 1 or 0.

Variables Explained

Inputs and notation used for Fermat’s little theorem
SymbolMeaningInput rule
aThe integer base being raised to a power.Enter a whole number; it may be negative, zero or positive.
pThe modulus and the prime number in the theorem.Enter a prime integer of at least 2.
p − 1The exponent in the usual statement of the theorem.It is obtained automatically from p.
mod pThe operation that keeps the remainder after division by p.The result is an integer from 0 through p − 1.

Worked Example

Take a = 3 and p = 7. Since 7 is prime and does not divide 3, the theorem predicts a remainder of 1:

37−1 = 36 = 729. Dividing 729 by 7 gives 104 with a remainder of 1, so 36 ≡ 1 (mod 7).

Repeated squaring reaches the same answer with smaller values. First, 32 ≡ 2 (mod 7). Then 34 ≡ 22 ≡ 4 (mod 7). Finally, 36 = 34 × 32 ≡ 4 × 2 ≡ 8 ≡ 1 (mod 7). This is the method that scales to much larger exponents.

Why Use This Calculator?

A displayed remainder of 1 confirms the conclusion of the theorem for the supplied prime and a base not divisible by that prime. It is a check of a known consequence, rather than proof that a number is prime. Some composite numbers can also satisfy similar congruences for selected bases; those are Fermat pseudoprimes.

A result of 0 is expected when a is divisible by p. It does not contradict the theorem, because the version concluding “remainder 1” explicitly excludes that case. Use the equivalent ap congruence if you want a statement covering every integer base.

In hand calculations, the output can verify a modular reduction or help replace a large exponent with a smaller equivalent exponent. In cryptographic study, it illustrates one mathematical ingredient, but it is not an encryption, key-generation or security-testing tool.

Common Mistakes

  • Using a composite modulus: the familiar remainder-1 conclusion depends on p being prime.
  • Forgetting the divisibility condition: when p divides a, the reported remainder is 0.
  • Entering decimals: this theorem is stated for integers, so values such as 2.5 do not belong in either field.
  • Confusing equality with congruence: 36 is 729, not 1; it is congruent to 1 modulo 7.
  • Treating the test as a proof of primality: passing a Fermat congruence for one base does not rule out every composite number.
  • Calculating the full power first: reduce after each multiplication to keep hand calculations manageable and exact.

Frequently Asked Questions

Does a remainder of 1 prove that p is prime?

No. Every prime passes under the stated conditions, but certain composite numbers also produce a remainder of 1 for particular bases. A proper primality test needs additional checks.

What happens when a is larger than p?

Nothing special is required. The base can first be replaced by its remainder modulo p, because congruent bases produce congruent integer powers.

Can the base be negative?

Yes. Reduce the negative base to a nonnegative remainder first. For example, −2 is congruent to 5 modulo 7, so either representative gives the same final remainder.

Why must the inputs be whole numbers?

Divisibility, integer remainders and the stated congruence are integer concepts. A decimal base or modulus falls outside this version of the theorem.

Why is repeated squaring useful?

It finds a modular power with far fewer multiplications and keeps every intermediate value small. That preserves exactness for exponents whose full power would be impractical to write.

Is Fermat’s little theorem the same as Fermat’s last theorem?

No. The little theorem concerns modular powers and prime moduli. Fermat’s last theorem concerns integer solutions to a different equation involving powers greater than two.

What result should I expect if a equals p?

The result is 0 because p divides the base. The remainder-1 version does not apply, although the equivalent congruence a to the power p is congruent to a still holds.

Sources & References