ForHosting KIT · Math

Fermat's little theorem checker

Enter a base and a modulus and see whether a^(n−1) mod n equals 1: for 2 and 7 the remainder of 2^6 is 1, for 2 and 15 it is 4. Useful for checking homework or spotting a composite counter-example. 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
A
2
N
7
You get
Result
1
First text
2
N
7
Exponent
6

Fermat's little theorem is the clean modular-arithmetic claim that if n is prime and base a is not divisible by n, then a raised to the power n minus one is congruent to one modulo n. Students, tutors, and developers constantly need a fast way to evaluate that exact power residue—not a prose recap of the proof—so they can check homework, probe composite counter-examples, or unit-test modular routines.

What Fermat's little theorem claims and what this tool computes

In its usual textbook form, Fermat's little theorem says: if n is a prime number and a is an integer not divisible by n, then a to the power n minus one is congruent to one modulo n. Written with the modular operator that programmers use every day, that is a^(n−1) mod n equals 1 whenever those hypotheses hold. You supply base a and modulus n.

The checker first requires n to be an integer at least two, because modular reduction with a modulus below two is not a useful classroom contract. It then requires that the greatest common divisor of a and n equal one—the coprimality hypothesis that matches the classical statement when n is prime (a not divisible by n) and still makes sense for composite n when you want to explore what happens outside the theorem's guarantee.

When both checks pass, the engine runs binary modular exponentiation (square-and-multiply) over BigInt so intermediate products stay exact even when the exponent n−1 is large.

Homework and classroom FLT checks

Students enter base a and prime n to confirm a^(n−1) mod n equals 1, or try composite n to see when the remainder is not 1.

Why must gcd(a, n) equal 1?

Fermat's little theorem is stated for bases coprime to the modulus (equivalently not divisible by prime n).

Does a result of 1 prove that n is prime?

No. When n is prime and gcd(a,n)=1 the remainder is always 1, but composites can also yield 1 for some bases. Use a dedicated primality tool for that question.

How do I check Fermat's little theorem with base 2 and modulus 7?

Enter 2 as the base and 7 as the modulus. The checker raises 2 to the 6th power, divides by 7 and returns a remainder of 1, so the theorem holds for this prime, exactly as expected.

Why does 2^14 mod 15 give 4 and not 1?

Because 15 is not prime, so the theorem promises nothing. Base 2 shares no common divisor with 15, the checker computes the remainder of 2 to the 14th power and gets 4, which shows that 15 fails the test.

Can I use a negative base or a very large modulus?

Yes. A negative base is first reduced modulo n, and the modulus only has to be a whole number of at least 2. The power is built by repeated squaring and multiplying with exact whole numbers, so even huge exponents give an exact remainder.

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/numth/fermat-little-check

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/numth/fermat-little-check \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"a":2,"n":7}'
{
  "a": 2,
  "n": 7
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "numth.fermat_little_check",
  "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.

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.

This Fermat's little theorem checker takes integers a and n, refuses the pair when gcd(a, n) is not one, and otherwise returns a^(n−1) mod n with a boolean flag telling you whether the remainder is one. The same pure BigInt logic powers the free browser tool and the paid API path at $0.002 per successful request, so classroom demos and production scripts never disagree on a residue.

What Fermat's little theorem claims and what this tool computes

The proof is not the product of this endpoint; the product is the exact remainder of that power. The response echoes a and n as decimal strings, reports the exponent n−1, the non-negative remainder result, gcd as the string "1", a boolean congruent_to_one that is true exactly when result equals 1, and a short expression string you can print next to the answer. Classic prime check: a equals 2 and n equals 7 yields 2^6 mod 7 equals 1, so congruent_to_one is true. Classic composite probe: a equals 2 and n equals 15 yields 2^14 mod 15 equals 4, so congruent_to_one is false—exactly the kind of counter-example that shows a^(n−1) ≡ 1 is not a primality certificate by itself.

Input contract, errors, and how to read the response

If n is less than two the task fails with a message that the modulus must be at least two. If gcd(a, n) is not one the task fails with a message that names the gcd—for example gcd(2, 4) equals 2—so callers know the pair is outside the coprime hypothesis rather than guessing why the power was not computed. Negative bases are accepted and reduced into the non-negative residue system modulo n before powering, which matches standard modular arithmetic. POST a JSON body with two required integer fields: a (aliases base) and n (aliases modulus, mod). Each field may be a JSON number or a decimal integer string; string form is preferred when values leave JavaScript's safe integer range. Fractions, empty strings, non-numeric tokens, and values longer than sixty-four decimal digits fail with invalid_input and are never billed. On success every numeric field in the body is a decimal string except congruent_to_one, which is a boolean, so large integers round-trip through JSON without precision loss. Pair this checker with a separate factoring or primality tool when your workflow needs more than the power residue; this endpoint answers only a^(n−1) mod n under the coprime gate so the SEO intent stays focused and the response stays minimal. Pricing is a flat $0.002 per successful request on the item unit; failed validation does not consume prepaid balance.

Classroom demos, composite probes, and API automation

Teachers use a Fermat little theorem checker so students can verify hand calculations of a^(p−1) mod p for small primes before they write a proof, and so they can deliberately try composite n and see that the remainder is not always one. Exam generators and LMS plugins need a deterministic oracle that rejects non-coprime input instead of silently returning a remainder that would confuse graders. Crypto-course notebooks often need a single exact modular power step when introducing RSA toy keys or illustrating why the exponent n−1 appears in elementary number theory before Euler's theorem generalizes the story with φ(n). Developers wire the API into property tests that assert a^(n−1) ≡ 1 for random bases coprime to a known prime modulus, or into regression suites that pin known composite counter-examples such as 2^14 mod 15 equals 4. Keep a and n within the published digit limit, pin the string-form result and congruent_to_one flag in your tests, and you get reproducible residues for homework, tutoring platforms, and edge workers without standing up a full computer-algebra system. When you later need Euler's theorem with φ(n), modular inverses, or Chinese remainder decompositions, call those separate capabilities; this one stays the dedicated Fermat a^(n−1) mod n landing so search intent and response shape remain clear.

LMS and exam auto-grading oracles

Mark modular-arithmetic items with a deterministic remainder and reject non-coprime pairs as invalid rather than inventing a success payload.

Unit tests for modular powering code

Scripts pin known residues such as 2^6 mod 7 = 1 and 2^14 mod 15 = 4 without reimplementing BigInt square-and-multiply in every language.

What does this Fermat's little theorem checker compute?

Given integers a and n with n ≥ 2 and gcd(a,n)=1, it returns a^(n−1) mod n, plus whether that remainder equals 1 (congruent_to_one).

What happens if n is composite?

The checker still computes a^(n−1) mod n when gcd(a,n)=1. congruent_to_one may be true or false; false cases are useful classroom counter-examples.

What does the Fermat little theorem checker cost?

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

Read the full KIT documentation →