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 says that if p is prime and an integer a is not divisible by p, then ap−1 ≡ 1 (mod p). The congruence symbol means the two sides leave the same remainder after division by p.

This calculator checks that p is prime, calculates a^(p−1) modulo p and explains whether the remainder-one condition applies. It accepts a negative base too. It does not accept a custom exponent, compute a modular inverse directly or prove primality merely because a single power leaves remainder 1.

How to Calculate a Power Modulo a Prime

  1. Enter a signed whole number in Integer base a, using plain digits and an optional minus sign.
  2. Enter a prime integer from 2 through 2,000,000,000 in Prime modulus p. Composite moduli are rejected.
  3. Select Calculate. The exponent is automatically p − 1, not a separate input.
  4. Read the modular expression, remainder and Condition check. A base divisible by p is accepted, but its positive power has remainder 0 rather than 1.

The calculator first checks p by trial division, reduces a to a nonnegative residue modulo p and uses repeated squaring to evaluate the power. It reduces intermediate products modulo p instead of constructing the enormous full power.

The supported modulus range keeps these modular products within signed 64-bit integer capacity on this runtime. The base must also fit the server’s native integer validation. Enter integers without decimal points, grouping commas or scientific notation. No physical units belong on these fields.

Fermat’s Little Theorem Formula and Conditions

If p is prime and p does not divide a: a^(p − 1) ≡ 1 (mod p)

For every integer a and prime p: a^p ≡ a (mod p)

If p divides a: a^(p − 1) mod p = 0, since p ≥ 2

Normalized base r = ((a mod p) + p) mod p

The general a^p form includes bases divisible by p. The remainder-one form has the additional nondivisibility condition. The calculator evaluates only the p − 1 exponent, so a zero result for a multiple of p does not contradict the theorem.

Repeated squaring uses the exponent’s binary digits. At each step, square the current residue, reduce it modulo p and multiply it into an accumulating result when the corresponding binary digit is 1. This computes the same remainder with bounded intermediate values.

Reference: University of Connecticut: Fermat’s Little Theorem, Keith Conrad. Prime-modulus congruence and its nonzero-residue condition. Input rules, rounding and result presentation described here follow this calculator’s implementation.

Integer Base, Prime Modulus and Remainder

Integer base a
Signed native-range whole number whose p − 1 power is evaluated.
Prime modulus p
Prime divisor defining the remainders, between 2 and 2,000,000,000.
Modular result
Featured expression showing a, exponent p − 1, modulus p and the calculated remainder.
Remainder
Nonnegative residue after division by p. For an accepted prime p it is 1 when a is not divisible by p, otherwise 0.
Normalized base
Residue of a in the interval 0 through p − 1, including for negative a.
Exponent p − 1
Automatic exponent determined by p, not a user-selected power.
Condition check
Explains whether a is divisible by p and therefore whether the remainder-one form applies.

Fermat’s Little Theorem Worked Example

Illustrative example. Enter a = 5 and p = 11. Eleven is prime, and 5 is not divisible by 11, so Fermat’s remainder-one condition applies.

The automatic exponent is 10. Compute residues: 5² ≡ 3, 5⁴ ≡ 9 and 5⁸ ≡ 4 (mod 11). Then 5¹⁰ ≡ 5⁸ × 5² ≡ 4 × 3 = 12 ≡ 1 (mod 11).

Independently, 5¹⁰ = 9765625 = 11 × 887784 + 1. The calculator displays 5^10 mod 11 = 1. Changing a to 22 gives remainder 0 because 22 is divisible by 11.

Calculation check. These illustrative values were checked independently against the formula and calculator response. Retain unrounded intermediate values when checking approximations.

Fermat’s Little Theorem Worked Example
StepResidue or value
Base; prime modulus5; 11
Exponent p − 110
5² mod 113
5⁴ mod 119
5⁸ mod 114
5¹⁰ mod 111

Fermat’s Theorem Conditions and Prime Testing

Prime modulus and base divisibility answer different questions. The tool checks primality first, then reports whether a is divisible by that prime. It rejects composites instead of treating a Fermat remainder as a primality certificate.

For a hand-check counterexample, 341 = 11 × 31 is composite but 2¹⁰ = 1024 ≡ 1 (mod 341), so 2³⁴⁰ ≡ 1 too. A single remainder-one test cannot prove primality. Entering p = 341 here is rejected by the separate prime check.

Fermat’s Theorem Conditions and Prime Testing
InputsConditionExpected behavior
a = 5, p = 11Prime; base not divisible5^10 mod 11 = 1
a = 22, p = 11Prime; base divisible22^10 mod 11 = 0
a = −5, p = 11Prime; normalized base 6(−5)^10 mod 11 = 1
a = 2, p = 341Composite modulusRejected before theorem evaluation

What the Modular Remainder Tells You

A remainder of 1 confirms the expected congruence for this accepted prime and base. It is not the full value of a^(p−1). For example, a very large power can still leave the small remainder 1.

A remainder of 0 means the base is divisible by the accepted prime. The tool reports that the usual nondivisibility condition does not apply. This is a valid calculation case, not an assertion that p is composite.

A single Fermat congruence is not a general primality proof: some composite numbers pass it for particular bases. This tool separately checks p before calculating. Its small-modulus educational implementation and displayed expression should not be treated as a complete cryptographic system.

Common Fermat’s Theorem Mistakes

  • Using a composite modulus when applying a theorem that requires prime p.
  • Expecting remainder 1 when a is divisible by p.
  • Reading the remainder as the complete power.
  • Entering a custom exponent even though the tool fixes it at p − 1.
  • Assuming one successful Fermat congruence proves an arbitrary modulus prime.
  • Confusing Fermat’s little theorem with Fermat’s last theorem about sums of powers.

Frequently Asked Questions

What does Fermat’s little theorem say?

For prime p and an integer a not divisible by p, a^(p−1) has remainder 1 modulo p. The general form a^p ≡ a (mod p) also includes bases divisible by p.

What does the congruence symbol mean?

It means the two expressions have the same remainder when divided by the modulus. For example, 12 ≡ 1 (mod 11) because 12 = 11 + 1.

What happens when a is divisible by p?

The positive power a^(p−1) has remainder 0. The remainder-one condition does not apply, and the calculator explains this case in Condition check.

Can the base be negative?

Yes. The tool reduces it to a nonnegative residue first. For a = −5 and p = 11, the normalized base is 6 and the tenth power leaves remainder 1.

Can the modulus be composite?

No. This calculator checks that p is prime and rejects composite values. It supports prime moduli from 2 through 2,000,000,000.

Does a Fermat remainder of 1 prove a number is prime?

No. Some composite numbers pass the congruence for particular bases. This tool uses a separate primality check rather than accepting one congruence as proof.

Does this calculate any exponent or a modular inverse?

No. The exponent is fixed at p − 1. Use a power-modulo or modular-inverse tool for those different tasks.

How can it evaluate such a large power without displaying the full integer?

It repeatedly squares residues and reduces products modulo p. This preserves the final remainder without constructing the entire power.

Sources and References