Lably
Skip to content

Primality test

Algorithm for determining whether a number is prime

  • 12 min read
  • 6 chapters
  • 6 labs on Lably

Learn it on Lably

Simple methods

The simplest primality test is trial division: given an input number, n, check whether it is divisible by any prime number between 2 and n (i.e., whether the division leaves no remainder). If so, then n is composite. Otherwise, it is prime. All divisors p≥n, must have a divisor np≤n, and a prime divisor q of np, and therefore looking for prime divisors at most n is sufficient.

For example, consider the number 100, whose divisors are these numbers:

1, 2, 4, 5, 10, 20, 25, 50, 100.

When all possible divisors up to n are tested, some divisors will be discovered twice. To observe this, consider the list of divisor pairs of 100:

1×100,2×50,4×25,5×20,10×10,20×5,25×4,50×2,100×1.

Products past 10×10 are the reverse of products that appeared earlier. For example, 5×20 and 20×5 are the reverse of each other. Further, that of the two divisors, 5≤100=10 and 20≥100=10. This observation generalizes to all n: all divisor pairs of n contain a divisor less than or equal to n, so the algorithm need only search for divisors less than or equal to n to guarantee detection of all divisor pairs.

Also, 2 is a prime dividing 100, which immediately proves that 100 is not prime. Every positive integer except 1 is divisible by at least one prime number by the Fundamental Theorem of Arithmetic. Therefore the algorithm need only search for prime divisors less than or equal to n.

For another example, consider how this algorithm determines the primality of 17. One has 4<17<5, and the only primes ≤17 are 2 and 3. Neither divides 17, proving that 17 is prime. For a last example, consider 221. One has 14<221<15, and the primes ≤221 are 2, 3, 5, 7, 11, and 13. Upon checking each, one discovers that 22113=17, proving that 221 is not prime.

In cases where computing the list of primes ≤n is not feasible, all numbers between 2 and n can be simply (and slowly) checked for divisors. A simple improvement is to test divisibility by 2 and by just the odd numbers between 3 and n, since divisibility by an even number implies divisibility by 2.

This method can be improved further. Observe that all primes greater than 5 are of the form 6k+i for a nonnegative integer k and i∈{1,5}. Indeed, every integer is of the form 6k+i for a positive integer k and i∈{0,1,2,3,4,5}. Since 2 divides 6k,6k+2, and 6k+4, and 3 divides 6k and 6k+3, the only possible remainders mod 6 for a prime greater than 3 are 1 and 5. So, a more efficient primality test for n is to test whether n is divisible by 2 or 3, then to check through all numbers of the form 6k+1 and 6k+5 which are ≤n. This is almost three times as fast as testing all numbers up to n.

Generalizing further, all primes greater than pm (the mth prime number) are of the form pm#⋅k+i, where {k,i∈Z∣0≤i<pm#∧gcd(pm#,i)=1} and pm# is the primorial – the product of the first m primes.

For example, consider p3#=2⋅3⋅5=30. All integers are of the form 30k+i, where {k,i∈Z∣0≤i<30}. Now, 2 divides 0,2,4,…,28, 3 divides 0,3,6,…,27, and 5 divides 0,5,10,…,25. Thus all prime numbers greater than 30 are of the form 30k+i for i∈{1,7,11,13,17,19,23,29}. Of course, not all numbers of the form pm#⋅k+i with i coprime to pm# are prime. For example, 19⋅23=437=210⋅2+17=p4#⋅2+17 is not prime, even though 17 is coprime to p4#=2⋅3⋅5⋅7.

While limm→∞ϕ(pm#)pm#=0, where ϕ(m) is the Euler totient function, checking for divisibility by all primes that are less than pm# is still needed. Observations analogous to the preceding can be applied recursively, giving the Sieve of Eratosthenes.

One way to speed up these methods (and all the others mentioned below) is to pre-compute and store a list of all primes up to a certain bound, such as all primes up to 200. (Such a list can be computed with the Sieve of Eratosthenes or by an algorithm that tests each incremental m against all known primes ≤m). Then, before testing n for primality with a large-scale method, n can first be checked for divisibility by any prime from the list. If it is divisible by any of those numbers then it is composite, and any further tests can be skipped.

A simple but inefficient primality test uses Wilson's theorem, which states that p is prime if and only if:

(p−1)!≡−1(modp)

Although this method requires about p modular multiplications, rendering it impractical, theorems about primes and modular residues form the basis of many more practical methods.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Number theory