ForHosting KIT · Math

Fermat's little theorem calculator

2 to the 6th modulo 7 leaves 1, exactly as Fermat's little theorem predicts. Enter a base a and a prime p to get the residue of a to the p minus 1 modulo p; composites are refused, never passed off as primes. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Base
2
Prime
7
You get
Reduced base
2
Exponent
6
Residue
1
Expression
2^{6} ≡ 1 (mod 7)

Fermat's little theorem is the number-theory fact that if p is a prime and a is an integer not divisible by p, then a to the power p minus one is congruent to one modulo p. That identity is the engine behind Fermat primality tests, modular inverses in a prime field, and contest problems that ask you to reduce a huge power without expanding it. This calculator takes a base a and a prime modulus p, checks that p is actually prime, then returns the exact residue of a to the p minus one modulo p using deterministic BigInt modular exponentiation. When a and p are coprime the residue is one, which is the theorem; when p divides a the residue is zero.

What Fermat's little theorem states and what the residue means

Fermat's little theorem says that if p is a prime number and a is an integer that is not a multiple of p, then a raised to the power p minus one is congruent to one modulo p. In symbols that is a to the p minus one congruent to one mod p. An equivalent form, which holds even when p divides a, is a to the p congruent to a modulo p.

The first form is the one this capability evaluates: it computes the residue of a to the p minus one modulo p and returns that residue as the primary result. When a and p are coprime the residue is always one, which is the theorem; that is the check students write on homework and the identity used to invert a modulo p by raising a to the p minus two.

When p divides a the power collapses to zero modulo p because every positive power of a multiple of p is still a multiple of p, so the residue is zero and the coprime flag is false. Negative bases are accepted and reduced into the range zero through p minus one before the power is taken, which keeps the remainder non-negative and matches the usual classroom convention for modular arithmetic.

Where this check is most useful

Number-theory courses and contest platforms use a Fermat's little theorem calculator so learners can confirm a to the p minus one modulo p without implementing modular exponentiation by hand and without confusing a composite Fermat liar with a prime. Research notebooks and recreational scripts call the same contract when they want an independent oracle for a published example: two to the sixth modulo seven must be one, three to the fourth modulo five must be one, and a composite modulus such as nine must be rejected rather than reduced.

Cryptography workshops that introduce Fermat primality tests or inverses in a prime field need a tool that refuses a composite modulus, because the pedagogical point is the theorem's hypothesis, not a silent modular power that happens to look plausible. Failed validation does not consume balance. Treat a not-prime error as a first-class outcome in your suite: it is the contract that keeps a Fermat liar from grading as a pass.

Classroom and homework checks

Let students submit a base and a claimed prime and verify that a^(p−1) mod p is one when the pair is coprime, or that a composite modulus is rejected.

Crypto labs and prime-field inverses

Confirm a^(p−2) as the inverse of a modulo prime p by first checking that a^(p−1) ≡ 1 (mod p) on the same pair, without embedding a modular-pow routine.

What happens if the modulus is not prime?

Fermat's little theorem requires a prime modulus; composite values are not reduced and are not billed.

When is the residue equal to one?

Whenever p is prime and gcd(a, p) = 1. That is the classical statement of Fermat's little theorem. If p divides a, the residue is zero.

Are negative bases accepted?

Yes. A negative base is reduced into the range 0 through p−1 before exponentiation, so the remainder is the usual non-negative residue.

What happens when the base is a multiple of p?

The residue is 0, because every positive power of a multiple of p is still a multiple of p. That is the one case where the theorem's condition fails, and the result shows that a and p are not coprime.

How is the theorem used to invert a number modulo p?

When a and p share no factor, a to the power p minus 2 is the inverse of a modulo p, because multiplying it by a gives a to the p minus 1, which leaves 1. This calculator confirms that residue of 1.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/math/fermats-little-theorem

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/math/fermats-little-theorem \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"base":2,"prime":7}'
{
  "base": 2,
  "prime": 7
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "math.fermats_little_theorem",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_abs9007199254740991
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Composite moduli are rejected with invalid_input instead of a misleading remainder, so homework scripts never treat a Fermat-liar composite as a prime.

What Fermat's little theorem states and what the residue means

The calculator also returns the exponent p minus one, a boolean that records whether a and p share no common factor, a congruent_to_one flag that is true exactly when the residue is one, and a short congruence string you can print next to the answer. Composite moduli are not reduced: the task fails with invalid_input so a Fermat liar such as three hundred forty-one never masquerades as a prime.

Input contract, algorithm, and error behaviour

Absolute values are bounded by the JavaScript safe-integer limit so every intermediate modular product stays exact under BigInt. Example: base two and prime seven yields residue one, because two to the sixth is sixty-four and sixty-four minus nine times seven is one. Example: base ten and prime five yields residue zero, because five divides ten. POST a JSON body with two required integer fields: base, the integer a of the power, and prime, the modulus p which must be a prime at least two. Aliases a for base and p, modulus, or mod for prime are accepted so classroom models that already label the pair that way do not need a rewrite. Each field may arrive as a JSON integer or as a digit string; booleans, empty strings, fractions, and non-finite values fail with invalid_input naming the field. If p is composite, one, zero, or negative, the request fails with a message that names prime and states that the value is not prime; those failures are not billed. On success the body echoes base and prime, reports reduced_base equal to a modulo p in zero through p minus one, reports exponent equal to p minus one, and reports residue equal to reduced_base to that exponent modulo p. The flags coprime and congruent_to_one let a grader assert the classical statement without re-deriving the power. Pair the response with your own notes if you need a written proof; this endpoint returns the residue and the flags, not a step-by-step prose derivation.

Where a Fermat residue API earns its keep

Keep requests inside the published integer bounds, pin the residue and the congruent_to_one flag in tests, and you get a reproducible Fermat residue for homework, LMS plugins, and edge workers without standing up a computer-algebra system of your own.

Exam and LMS auto-grading

Mark Fermat little-theorem items with a deterministic oracle that fails composite moduli as invalid_input instead of returning a remainder that a buggy grader might treat as success.

What does this Fermat's little theorem calculator return?

Send integers base (a) and prime (p). The API returns residue = a^(p−1) mod p, plus reduced_base, exponent p−1, coprime, congruent_to_one, and a short congruence string.

What does the Fermat's little theorem calculator cost?

The browser tool is free. API requests use prepaid KIT balance at $0.002 per successful call; invalid input is not billed.

Read the full KIT documentation →