Skip to content

Primality Testing

A prime number is an integer greater than $1$ whose only positive divisors are

The Problem of Recognizing Primes

A prime number is an integer greater than 11 whose only positive divisors are

1andp. 1 \quad\text{and}\quad p.

Prime numbers are the fundamental building blocks of arithmetic because every integer factors uniquely into primes.

One of the oldest computational problems in number theory is therefore:

Given an integer nn, determine whether nn is prime.

This is the primality testing problem.

For small integers, direct trial division suffices. For very large integers, especially those used in cryptography, sophisticated algorithms are required.

Modern primality testing combines algebra, modular arithmetic, analytic number theory, and computational complexity theory.

Trial Division

The simplest method tests divisibility by integers up to

n. \sqrt{n}.

If no divisor is found, then nn is prime.

This works because if

n=ab, n=ab,

then at least one of aa or bb satisfies

an. a\leq\sqrt{n}.

Although conceptually simple, trial division is extremely inefficient for large integers. Its running time grows exponentially in the bit length of the input.

Thus more advanced methods are necessary.

Fermat’s Little Theorem

One of the first major tools in primality testing is Fermat’s little theorem.

If pp is prime and

a≢0(modp), a\not\equiv0\pmod p,

then

ap11(modp) a^{p-1}\equiv1\pmod p

This gives a necessary condition for primality.

If an integer nn fails the congruence

an11(modn), a^{n-1}\equiv1\pmod n,

then nn is certainly composite.

Numbers satisfying the congruence for a particular base aa are called pseudoprimes to base aa.

Carmichael Numbers

Unfortunately, some composite numbers satisfy Fermat congruences for many bases.

A Carmichael number satisfies

an11(modn) a^{n-1}\equiv1\pmod n

for every aa coprime to nn.

The smallest example is

561=31117. 561=3\cdot11\cdot17.

Carmichael numbers show that Fermat testing alone cannot provide a reliable primality test.

Miller-Rabin Primality Test

The Miller-Rabin test strengthens Fermat’s criterion.

Suppose

n1=2sd n-1=2^sd

with dd odd.

For a chosen base aa, one examines the sequence

ad,a2d,a4d,,a2sd(modn). a^d, \quad a^{2d}, \quad a^{4d}, \ldots, \quad a^{2^sd} \pmod n.

If nn is prime, this sequence must satisfy specific structural conditions.

Failure of these conditions proves compositeness.

The Miller-Rabin test is probabilistic:

  • if it declares composite, the answer is certain,
  • if it declares probable prime, the answer is extremely likely but not absolutely guaranteed.

Repeating the test with multiple bases reduces error probability exponentially.

In practice, Miller-Rabin is extremely effective and widely used.

Deterministic Variants

For integers below certain bounds, carefully chosen bases make Miller-Rabin deterministic.

For example, all 6464-bit integers can be tested deterministically using only finitely many bases.

Thus practical implementations often combine deterministic small-base testing with probabilistic methods.

Solovay-Strassen Test

Another probabilistic method uses Euler’s criterion and quadratic residues.

If pp is prime, then

a(p1)/2(ap)(modp), a^{(p-1)/2} \equiv \left(\frac{a}{p}\right) \pmod p,

where

(ap) \left(\frac{a}{p}\right)

is the Legendre symbol.

The Solovay-Strassen test compares these quantities modulo nn.

Although historically important, it is generally slower and less practical than Miller-Rabin.

Lucas Tests

Lucas primality tests use recurrence sequences and properties of finite groups.

These tests are particularly effective for numbers of special forms.

Many practical primality-proving algorithms combine Miller-Rabin tests with Lucas tests.

The resulting hybrid methods are extremely reliable.

AKS Primality Test

A major breakthrough occurred in 2002 when entity[“people”,“Manindra Agrawal”,“Indian computer scientist”], entity[“people”,“Neeraj Kayal”,“Indian computer scientist”], and entity[“people”,“Nitin Saxena”,“Indian computer scientist”] discovered the AKS primality test.

The AKS algorithm proved that primality testing belongs to deterministic polynomial time.

The key identity is:

(xa)pxpa(modp) (x-a)^p\equiv x^p-a\pmod p

for prime pp.

The algorithm verifies suitable polynomial congruences modulo nn.

This result solved a long-standing theoretical problem:

PRIMESP. \text{PRIMES}\in\mathbf{P}.

Although AKS is theoretically profound, it is usually slower in practice than probabilistic methods.

Elliptic Curve Primality Proving

Elliptic curves provide some of the fastest practical primality-proving algorithms.

The elliptic curve primality proving method (ECPP) uses arithmetic on elliptic curves over finite fields.

These algorithms can produce certificates of primality that may be verified efficiently.

ECPP is widely used for proving primality of very large integers.

Special-Form Primes

Some primes possess forms allowing especially efficient tests.

Mersenne Primes

Numbers of the form

2p1 2^p-1

are called Mersenne numbers.

The Lucas-Lehmer test determines primality of Mersenne numbers very efficiently.

The largest known primes are often Mersenne primes discovered through distributed computation projects.

Fermat Numbers

Numbers of the form

22n+1 2^{2^n}+1

are Fermat numbers.

Only a few are known to be prime.

Complexity Considerations

Primality testing differs fundamentally from factorization.

Primality can be tested efficiently, but factoring large integers remains computationally difficult.

This asymmetry underlies RSA cryptography.

Given a large integer NN:

  • testing whether NN is prime is easy,
  • factoring NN may be extremely hard.

This distinction is one of the central computational facts of modern number theory.

Cryptographic Applications

Prime numbers are essential in cryptography.

Examples include:

SystemRole of Primes
RSALarge secret primes
Diffie-HellmanPrime finite fields
Elliptic curvesPrime-order groups
Digital signaturesPrime moduli

Practical cryptography therefore depends heavily on fast and reliable primality testing.

Random Prime Generation

Cryptographic systems often require random large primes.

The standard method is:

  1. generate a random odd integer,
  2. apply probabilistic primality tests,
  3. repeat until a probable prime appears.

The prime number theorem implies that primes occur frequently enough for this approach to be efficient.

Conceptual Importance

Primality testing illustrates a major theme of computational number theory:

deep theoretical mathematics can lead to highly practical algorithms.

The subject combines:

  • modular arithmetic,
  • finite fields,
  • algebraic geometry,
  • complexity theory,
  • probability.

Primality testing also reveals an important distinction between existence and construction. Although primes are infinite and theoretically well understood, recognizing them efficiently required centuries of mathematical development.