Miller-Rabin Primality Test Calculator

Test a large integer for primality with the Miller-Rabin witness method.

Math and PercentUpdated 2026-11-203 min read
Miller-Rabin Primality Test Calculator
Whole numbers up to 16 digits. Odd numbers above 2 are tested.
Result
...
Witness or none...
Detail...

What it does

A primality test answers a simple question: is this integer prime? Trial division works for small numbers but becomes hopeless once the number has many digits. The Miller-Rabin test uses modular arithmetic instead, so it stays fast even for large inputs.

The method was published by Gary Miller in 1976 and turned into a practical randomized test by Michael Rabin in 1980. It strengthens the older Fermat test. Fermat's little theorem says that for a prime n and 1 < a < n, a to the power n-1 leaves remainder 1 modulo n. Some composite numbers, called Carmichael numbers, pass that check for every base a that is coprime to n.

Miller-Rabin adds a second condition. Write n-1 as 2 to the power s times an odd number d. For each chosen base a, compute a to the power d modulo n. The number passes that base if the result is 1, or if repeated squaring reaches n-1 before the end. If neither happens, the base is a witness that n is composite.

This tool reports the result and the witness when there is one. The engine uses the first twelve prime bases and BigInt arithmetic, so there is no floating point rounding in the modular exponentiation. Even inputs are rejected as composite before the test runs.

For the range accepted here, the base set is more than enough. Mathematicians have tabulated small base sets that are deterministic up to very large bounds, and the first twelve prime bases cover numbers far larger than 16 digits. A Prime result can therefore be read as an exact answer for this calculator.

How to use

  1. Enter an integer of at most 16 digits. Decimals, signs and letters are rejected.
  2. Choose how many of the small prime bases to try. Five is the default and is ample for this range.
  3. Read the result. Composite means a witness was found, so the answer is certain.
  4. Check the witness field to see which base proved compositeness, or none for a prime result.
  5. Try 561 to see the classic Carmichael case, then try 104729 to see a prime.
  6. Use the detail line to confirm which branch of the test produced the answer.

Formula

Write n-1 = 2^s × d with d odd. For each base a, n passes if a^d ≡ 1 (mod n) or a^(2^r × d) ≡ -1 (mod n) for some 0 <= r < s. Otherwise a is a witness and n is composite.
n
The odd integer being tested, greater than 2.
a
A base, here one of the small primes 2, 3, 5, 7 and so on.
s
The number of times 2 divides n-1.
d
The odd part of n-1, found by dividing out all factors of 2.
mod
Remainder after division, computed on exact integers.

The engine factors n-1 into a power of two and an odd part, then uses repeated squaring to keep every intermediate value below n. Even input returns Composite with witness 2. Inputs below 3 are rejected because the theorem needs n greater than 2.

Worked example

Example: testing 104729

Input 104729 with five bases. This is a known prime near the hundred thousand mark.

  1. The input passes the format check: 104729 is an integer of 6 digits.
  2. It is odd, so the engine does not return the even number shortcut.
  3. Factor n-1 = 104728 = 8 × 13091, so s = 3 and d = 13091.
  4. Base 2 is checked by computing 2^13091 mod 104729 and then squaring up to s-1 times. The chain passes the condition for this base.
  5. Bases 3, 5, 7 and 11 also pass, so no witness is found.
  6. The tool shows Result Prime, Witness none and Detail No witness found.

Reference table

How familiar numbers are classified
InputTypeResultWitness
97PrimePrimenone
561Carmichael numberComposite2
1105Carmichael numberComposite2
104729PrimePrimenone
1000003PrimePrimenone
1000000007PrimePrimenone

Limits

  • The input box accepts at most 16 digits because the page keeps the interface simple.
  • This tool tests a single integer. It does not factor composites or generate primes.
  • Primality is different from primality proving for arbitrary size. Larger inputs need specialised software.
  • The result says nothing about whether a number is a Mersenne prime or has any special form.

Common mistakes

  • Assuming Fermat's little theorem alone proves primality, which Carmichael numbers disprove.
  • Entering a decimal or a negative sign, both of which the engine rejects.
  • Reading a Composite result as probable when a witness is an outright proof.
  • Using far more bases than needed and thinking the answer is more certain than the mathematics allows.
  • Confusing an integer with its representation, for example entering 104729.0 as a different value.

Try it yourself

Quick checks. Answers sit tucked under each question.

What does the tool report for 561?

Composite, with witness 2. 561 is the smallest Carmichael number and fails the Fermat test for many bases.

What is the result for 104729 with five bases?

Prime, witness none. No tested base proves compositeness, and this base set is deterministic at that size.

Why is n-1 written as 2^s times d?

It separates the power of two so the repeated squaring chain can be checked one step at a time, which catches Carmichael numbers.

Questions

Is the Miller-Rabin test deterministic?

For bounded inputs, yes, if you use a base set with a proven bound. The small prime bases used here cover numbers far beyond the 16 digit limit of this page.

What is a witness?

A base a whose modular arithmetic proves that n is composite. One witness is enough for a certain Composite answer.

Why does the tool reject even numbers?

Every even number above 2 is composite, so it can be reported immediately without running the modular test.

What is a Carmichael number?

A composite number that passes Fermat's little theorem for every base coprime to it. Miller-Rabin catches these with the extra squaring condition.

How does repeated squaring help?

It keeps every intermediate product below n by reducing modulo n at each step, which makes large exponents cheap to compute exactly.

Can this tool find the factors of a composite?

No. It only decides whether the number is prime. Factoring is a different and harder problem.

Sources

How this page was checked. The formula above was matched against the cited sources, and the worked example was run through the tool on this page before publishing. If you spot an error, contact us so we can fix it and note the correction.