Home▸Articles▸Algorithms & AI

The Miller–Rabin Primality Test: A Probabilistic Approach to Prime Verification

A cornerstone in computational number theory, used for cryptographic applications and beyond.

mysimulator teamUpdated June 2026≈ 3 min read▶ Open the simulation

What the Miller–Rabin Primality Test Is

The Miller–Rabin primality test is an algorithm designed to determine whether a given odd integer n is prime. It operates on the principle that for any composite number, there are witnesses (numbers) that can prove its compositeness through specific mathematical operations.

Unlike deterministic tests which either conclusively prove or disprove primality, the Miller–Rabin test provides probabilistic results with a high degree of confidence.

How It Works

The test leverages properties from number theory, particularly Fermat's Little Theorem and the fact that for any prime p, (a^(p-1)) mod p = 1. For composite numbers, this equation does not hold for most bases a.

In practice, the algorithm involves expressing n-1 as 2^s * d where s is an integer and d is odd. It then checks if a^d ≡ 1 (mod n) or if there exists some j such that a^(2^j * d) ≡ -1 (mod n). If neither condition holds for any base a, the number is considered composite.

live demo · related simulation● LIVE

Why It Matters

The Miller–Rabin test is crucial in cryptography and computer science because it allows efficient verification of large prime numbers, which are essential for generating secure cryptographic keys.

Its probabilistic nature ensures that the risk of false positives can be made arbitrarily small by increasing the number of rounds k, making it a robust choice for practical applications.

Real-World Applications

The test is widely used in generating large prime numbers for RSA encryption and other public-key cryptography systems. It ensures that the primes chosen are sufficiently random and large enough to resist factorization by modern computers.

Beyond cryptography, it also finds applications in various computational problems requiring prime number verification.

Frequently asked questions

What is a witness in the Miller–Rabin test?

A witness in the Miller–Rabin test is an integer a that helps prove the compositeness of a given number n. If a satisfies certain conditions, it can be used to show that n is not prime.

How does the test handle Carmichael numbers?

Carmichael numbers are composite numbers that pass the Fermat primality test for all bases a coprime with them. The Miller–Rabin test, however, can still identify these numbers by using multiple rounds and different bases.

Can the test be made deterministic?

While theoretically possible to make the test deterministic, it would require checking all potential witnesses, which is computationally infeasible for large numbers. Thus, the probabilistic nature remains practical and efficient.

What is the significance of increasing the number of rounds k?

Increasing k reduces the probability of a false positive (a composite number being incorrectly identified as prime). This ensures that the test's confidence in its results can be adjusted according to the required level of certainty.

Try it live

Everything above runs in your browser — open Miller–Rabin Primality Test and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.

▶ Open Miller–Rabin Primality Test simulation

What did you find?

Add reproduction steps (optional)