A primality test is an algorithm for determining whether an input number is prime. Among other fields of mathematics, it is used for cryptography. Unlike integer factorization, primality tests do not generally give prime factors, only stating whether the input number is prime or not. Factorization is thought to be a computationally difficult problem, whereas primality testing is comparatively easy (its running time is polynomial in the size of the input). Some primality tests prove that a number is prime, while others like Miller–Rabin prove that a number is composite. Therefore, the latter might more accurately be called compositeness tests instead of primality tests.

## Simple methods

The simplest primality test is trial division: given an input number, ${\displaystyle n}$, check whether it is divisible by any prime number between 2 and ${\displaystyle {\sqrt {n))}$ (i.e., whether the division leaves no remainder). If so, then ${\displaystyle n}$ is composite. Otherwise, it is prime.[1] In fact, for any divisor ${\displaystyle p\geq {\sqrt {n))}$, there must be another divisor ${\displaystyle n/p\leq {\sqrt {n))}$, and a prime divisor ${\displaystyle q}$ of ${\displaystyle n/p}$, and therefore looking for prime divisors at most ${\displaystyle {\sqrt {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 ${\displaystyle n}$ are tested, some divisors will be discovered twice. To observe this, consider the list of divisor pairs of 100:

${\displaystyle 1\times 100,\;2\times 50,\;4\times 25,\;5\times 20,\;10\times 10,\;20\times 5,\;25\times 4,\;50\times 2,\;100\times 1}$.

Notice that products past ${\displaystyle 10\times 10}$ are the reverse of products which appeared earlier. For example, ${\displaystyle 5\times 20}$ and ${\displaystyle 20\times 5}$ are the reverse of each other. Note further that of the two divisors, ${\displaystyle 5\leq {\sqrt {100))=10}$ and ${\displaystyle 20\geq {\sqrt {100))=10}$. This observation generalizes to all ${\displaystyle n}$: all divisor pairs of ${\displaystyle n}$ contain a divisor less than or equal to ${\displaystyle {\sqrt {n))}$, so the algorithm need only search for divisors less than / equal to ${\displaystyle {\sqrt {n))}$ to guarantee detection of all divisor pairs.[1]

Also notice that 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 / equal to ${\displaystyle {\sqrt {n))}$.

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

In cases where it is not feasible to compute the list of primes ${\displaystyle \leq {\sqrt {n))}$, it is also possible to simply (and slowly) check all numbers between ${\displaystyle 2}$ and ${\displaystyle {\sqrt {n))}$ for divisors. A rather simple optimization is to test divisibility by 2 and by just the odd numbers between 3 and ${\displaystyle {\sqrt {n))}$, since divisibility by an even number implies divisibility by 2.

This method can be improved further. Observe that all primes greater than 3 are of the form ${\displaystyle 6k+i}$ for a positive integer ${\displaystyle k}$ and ${\displaystyle i\in \{1,5\))$. Indeed, every integer is of the form ${\displaystyle 6k+i}$ for a positive integer ${\displaystyle k}$ and ${\displaystyle i\in \{0,1,2,3,4,5\))$. Since 2 divides ${\displaystyle 6k,6k+2}$, and ${\displaystyle 6k+4}$, and 3 divides ${\displaystyle 6k}$ and ${\displaystyle 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 ${\displaystyle n}$ is to test whether ${\displaystyle n}$ is divisible by 2 or 3, then to check through all numbers of the form ${\displaystyle 6k+1}$ and ${\displaystyle 6k+5}$ which are ${\displaystyle \leq {\sqrt {n))}$. This is almost three times as fast as testing all numbers up to ${\displaystyle {\sqrt {n))}$.

Generalizing further, all primes greater than ${\displaystyle c\#}$ (c primorial) are of the form ${\displaystyle c\#\cdot k+i}$ for ${\displaystyle i,k}$ positive integers, ${\displaystyle 0\leq i, and ${\displaystyle i}$ coprime to ${\displaystyle c\#}$. For example, consider ${\displaystyle 6\#=2\cdot 3\cdot 5=30}$. All integers are of the form ${\displaystyle 30k+i}$ for ${\displaystyle i,k}$ integers with ${\displaystyle 0\leq i<30}$. Now, 2 divides ${\displaystyle 0,2,4,\dots ,28}$, 3 divides ${\displaystyle 0,3,6,\dots ,27}$, and 5 divides ${\displaystyle 0,5,10,\dots ,25}$. Thus all prime numbers greater than 30 are of the form ${\displaystyle 30k+i}$ for ${\displaystyle i\in \{1,7,11,13,17,19,23,29\))$. Of course, not all numbers of the form ${\displaystyle c\#\cdot k+i}$ with ${\displaystyle i}$ coprime to ${\displaystyle c\#}$ are prime. For example, ${\displaystyle 19\cdot 23=437=210\cdot 2+17=2\cdot 7\#+17}$ is not prime, even though 17 is coprime to ${\displaystyle 7\#=2\cdot 3\cdot 5\cdot 7}$.

As ${\displaystyle c}$ grows, the fraction of coprime remainders to remainders decreases, and so the time to test ${\displaystyle n}$ decreases (though it still necessary to check for divisibility by all primes that are less than ${\displaystyle c}$). 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 ${\displaystyle m}$ against all known primes ${\displaystyle \leq {\sqrt {m))}$). Then, before testing ${\displaystyle n}$ for primality with a large-scale method, ${\displaystyle 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 very inefficient primality test uses Wilson's theorem, which states that ${\displaystyle p}$ is prime if and only if:

${\displaystyle (p-1)!\equiv -1{\pmod {p))}$

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

### Example code

#### Python

The following is a simple primality test in Python using the simple 6k ± 1 optimization mentioned earlier. More sophisticated methods described below are much faster for large n.

from math import isqrt
def is_prime(n: int) -> bool:
if n <= 3:
return n > 1
if n % 2 == 0 or n % 3 == 0:
return False
limit = isqrt(n)
for i in range(5, limit+1, 6):
if n % i == 0 or n % (i+2) == 0:
return False
return True


#### C, C++, C# & D

The following is a primality test in the C family of languages using the same optimization as above.

bool IsPrime(int n)
{
if (n == 2 || n == 3)
return true;

if (n <= 1 || n % 2 == 0 || n % 3 == 0)
return false;

for (int i = 5; i * i <= n; i += 6)
{
if (n % i == 0 || n % (i + 2) == 0)
return false;
}

return true;
}


Rust The following is a primality test in Rust (programming language) using same optimization as above.

fn is_prime(n: i32) -> bool {
if n == 2 || n == 3 {
return true;
}

if n <= 1 || n % 2 == 0 || n % 3 == 0 {
return false;
}

let mut i: i32 = 5;

while i * i <= n {
if n % i == 0 || n % (i + 2) == 0 {
return false;
}

i += 6;
}

true
}


#### Java

The following is a primality test in Java using the same optimization as above.

import java.util.*;

public static boolean isPrime(int n){

if (n <= 1)
return false;

if (n == 2 || n == 3)
return true;

if (n % 2 == 0 || n % 3 == 0)
return false;

for (int i = 5; i <= Math.sqrt(n); i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;

return true;
}


#### JavaScript

The following is a primality test in JavaScript using the same optimization as above.

function isPrime(num) {
if (num == 2 || num == 3)
return true;
if (num <= 1 || num % 2 == 0 || num % 3 == 0)
return false;
for (let i = 5; i * i <= num ; i+=6)
if (num % i == 0 || num % (i + 2) == 0)
return false;
return true;
}


#### R

The following is a primality test in R (programming language) using the same optimization as above.

is.prime <- function(number) {
if (number <= 1) {
return (FALSE)
} else if (number <= 3) {
return (TRUE)
}

if (number %% 2 == 0 || number %% 3 == 0) {
return (FALSE)
}

i <- 5
while (i*i <= number) {
if (number %% i == 0 || number %% (i+2) == 0) {
return (FALSE)
}
i = i + 6
}
return (TRUE)
}


#### Dart

The following is a primality test in Dart (programming_language) using the same optimization as above.

checkIfPrimeNumber(number) {
if (number == 2 || number == 3) {
return 'true';
} else if (number <= 1 || number % 2 == 0 || number % 3 == 0) {
return 'false';
}
for (int i = 5; i * i <= number; i += 6) {
if (number % i == 0 || number % (i + 2) == 0) {
return 'false';
}
}
return 'true';
}


#### Free Pascal

The following is a primality test in Free Pascal using the same optimization as above.

function IsPrime(N:Integer):Boolean;
var
I:Integer;
begin
if ((N = 2) or (N = 3)) then Exit(True);
if ((N <= 1) or (N mod 2 = 0) or (N mod 3 = 0)) then Exit(False);
I := 5;
while (I * I <= N) do
begin
if ((N mod I = 0) or (N mod (I+2) = 0)) then Exit(False);
Inc(I, 6);
end;
Exit(True);
end;


#### Go

The following is a primality test in Golang using the same optimization as above.

func IsPrime(num int) bool {
if num > 1 && num <= 3 {
return true
}
if num <= 1 || num%2 == 0 || num%3 == 0 {
return false
}

for i := 5; i*i <= num; i += 6 {
if num%i == 0 || num%(i+2) == 0 {
return false
}
}
return true
}


The following is a primality test in Haskell using the same optimization as above.

isPrime :: Int -> Bool

## Probabilistic tests

Probabilistic tests are more rigorous than heuristics in that they provide provable bounds on the probability of being fooled by a composite number. Many popular primality tests are probabilistic tests. These tests use, apart from the tested number n, some other numbers a which are chosen at random from some sample space; the usual randomized primality tests never report a prime number as composite, but it is possible for a composite number to be reported as prime. The probability of error can be reduced by repeating the test with several independently chosen values of a; for two commonly used tests, for any composite n at least half the a's detect n's compositeness, so k repetitions reduce the error probability to at most 2k, which can be made arbitrarily small by increasing k.

The basic structure of randomized primality tests is as follows:

1. Randomly pick a number a.
2. Check equality (corresponding to the chosen test) involving a and the given number n. If the equality fails to hold true, then n is a composite number and a is a witness for the compositeness, and the test stops.
3. Get back to the step one until the required accuracy is reached.

After one or more iterations, if n is not found to be a composite number, then it can be declared probably prime.

### Fermat primality test

The simplest probabilistic primality test is the Fermat primality test (actually a compositeness test). It works as follows:

Given an integer n, choose some integer a coprime to n and calculate an − 1 modulo n. If the result is different from 1, then n is composite. If it is 1, then n may be prime.

If an−1 (modulo n) is 1 but n is not prime, then n is called a pseudoprime to base a. In practice, we observe that, if an−1 (modulo n) is 1, then n is usually prime. But here is a counterexample: if n = 341 and a = 2, then

${\displaystyle 2^{340}\equiv 1{\pmod {341))}$

even though 341 = 11·31 is composite. In fact, 341 is the smallest pseudoprime base 2 (see Figure 1 of [3]).

There are only 21853 pseudoprimes base 2 that are less than 2.5×1010 (see page 1005 of [3]). This means that, for n up to 2.5×1010, if 2n−1 (modulo n) equals 1, then n is prime, unless n is one of these 21853 pseudoprimes.

Some composite numbers (Carmichael numbers) have the property that an − 1 is 1 (modulo n) for every a that is coprime to n. The smallest example is n = 561 = 3·11·17, for which a560 is 1 (modulo 561) for all a coprime to 561. Nevertheless, the Fermat test is often used if a rapid screening of numbers is needed, for instance in the key generation phase of the RSA public key cryptographic algorithm.

### Miller–Rabin and Solovay–Strassen primality test

The Miller–Rabin primality test and Solovay–Strassen primality test are more sophisticated variants, which detect all composites (once again, this means: for every composite number n, at least 3/4 (Miller–Rabin) or 1/2 (Solovay–Strassen) of numbers a are witnesses of compositeness of n). These are also compositeness tests.

The Miller–Rabin primality test works as follows: Given an integer n, choose some positive integer a < n. Let 2sd = n − 1, where d is odd. If

${\displaystyle a^{d}\not \equiv \pm 1{\pmod {n))}$

and

${\displaystyle a^{2^{r}d}\not \equiv -1{\pmod {n))}$ for all ${\displaystyle 0\leq r\leq s-1,}$

then n is composite and a is a witness for the compositeness. Otherwise, n may or may not be prime. The Miller–Rabin test is a strong probable prime test (see PSW[3] page 1004).

The Solovay–Strassen primality test uses another equality: Given an odd number n, choose some integer a < n, if

${\displaystyle a^{(n-1)/2}\not \equiv \left({\frac {a}{n))\right){\pmod {n))}$, where ${\displaystyle \left({\frac {a}{n))\right)}$ is the Jacobi symbol,

then n is composite and a is a witness for the compositeness. Otherwise, n may or may not be prime. The Solovay–Strassen test is an Euler probable prime test (see PSW[3] page 1003).

For each individual value of a, the Solovay–Strassen test is weaker than the Miller–Rabin test. For example, if n = 1905 and a = 2, then the Miller-Rabin test shows that n is composite, but the Solovay–Strassen test does not. This is because 1905 is an Euler pseudoprime base 2 but not a strong pseudoprime base 2 (this is illustrated in Figure 1 of PSW[3]).

### Frobenius primality test

The Miller–Rabin and the Solovay–Strassen primality tests are simple and are much faster than other general primality tests. One method of improving efficiency further in some cases is the Frobenius pseudoprimality test; a round of this test takes about three times as long as a round of Miller–Rabin, but achieves a probability bound comparable to seven rounds of Miller–Rabin.

The Frobenius test is a generalization of the Lucas probable prime test.

### Baillie–PSW primality test

The Baillie–PSW primality test is a probabilistic primality test that combines a Fermat or Miller–Rabin test with a Lucas probable prime test to get a primality test that has no known counterexamples. That is, there are no known composite n for which this test reports that n is probably prime.[4][5] It has been shown that there are no counterexamples for n ${\displaystyle <2^{64))$.

### Other tests

Leonard Adleman and Ming-Deh Huang presented an errorless (but expected polynomial-time) variant of the elliptic curve primality test. Unlike the other probabilistic tests, this algorithm produces a primality certificate, and thus can be used to prove that a number is prime.[6] The algorithm is prohibitively slow in practice.

If quantum computers were available, primality could be tested asymptotically faster than by using classical computers. A combination of Shor's algorithm, an integer factorization method, with the Pocklington primality test could solve the problem in ${\displaystyle O((\log n)^{3}(\log \log n)^{2}\log \log \log n)}$.[7]

## Fast deterministic tests

Near the beginning of the 20th century, it was shown that a corollary of Fermat's little theorem could be used to test for primality.[8] This resulted in the Pocklington primality test.[9] However, as this test requires a partial factorization of n − 1 the running time was still quite slow in the worst case. The first deterministic primality test significantly faster than the naive methods was the cyclotomy test; its runtime can be proven to be O((log n)c log log log n), where n is the number to test for primality and c is a constant independent of n. Many further improvements were made, but none could be proven to have polynomial running time. (Note that running time is measured in terms of the size of the input, which in this case is ~ log n, that being the number of bits needed to represent the number n.) The elliptic curve primality test can be proven to run in O((log n)6), if some conjectures on analytic number theory are true.[which?] Similarly, under the generalized Riemann hypothesis, the deterministic Miller's test, which forms the basis of the probabilistic Miller–Rabin test, can be proved to run in Õ((log n)4).[10] In practice, this algorithm is slower than the other two for sizes of numbers that can be dealt with at all. Because the implementation of these two methods is rather difficult and creates a risk of programming errors, slower but simpler tests are often preferred.

In 2002, the first provably unconditional deterministic polynomial time test for primality was invented by Manindra Agrawal, Neeraj Kayal, and Nitin Saxena. The AKS primality test runs in Õ((log n)12) (improved to Õ((log n)7.5)[11] in the published revision of their paper), which can be further reduced to Õ((log n)6) if the Sophie Germain conjecture is true.[12] Subsequently, Lenstra and Pomerance presented a version of the test which runs in time Õ((log n)6) unconditionally.[13]

Agrawal, Kayal and Saxena suggest a variant of their algorithm which would run in Õ((log n)3) if Agrawal's conjecture is true; however, a heuristic argument by Hendrik Lenstra and Carl Pomerance suggests that it is probably false.[11] A modified version of the Agrawal's conjecture, the Agrawal–Popovych conjecture,[14] may still be true.

## Complexity

In computational complexity theory, the formal language corresponding to the prime numbers is denoted as PRIMES. It is easy to show that PRIMES is in Co-NP: its complement COMPOSITES is in NP because one can decide compositeness by nondeterministically guessing a factor.

In 1975, Vaughan Pratt showed that there existed a certificate for primality that was checkable in polynomial time, and thus that PRIMES was in NP, and therefore in ${\displaystyle {\mathsf {NP\cap coNP))}$. See primality certificate for details.

The subsequent discovery of the Solovay–Strassen and Miller–Rabin algorithms put PRIMES in coRP. In 1992, the Adleman–Huang algorithm[6] reduced the complexity to ${\displaystyle {\mathsf ((\color {Blue}ZPP}=RP\cap coRP))}$, which superseded Pratt's result.

The Adleman–Pomerance–Rumely primality test from 1983 put PRIMES in QP (quasi-polynomial time), which is not known to be comparable with the classes mentioned above.

Because of its tractability in practice, polynomial-time algorithms assuming the Riemann hypothesis, and other similar evidence, it was long suspected but not proven that primality could be solved in polynomial time. The existence of the AKS primality test finally settled this long-standing question and placed PRIMES in P. However, PRIMES is not known to be P-complete, and it is not known whether it lies in classes lying inside P such as NC or L. It is known that PRIMES is not in AC0.[15]

## Number-theoretic methods

Certain number-theoretic methods exist for testing whether a number is prime, such as the Lucas test and Proth's test. These tests typically require factorization of n + 1, n − 1, or a similar quantity, which means that they are not useful for general-purpose primality testing, but they are often quite powerful when the tested number n is known to have a special form.

The Lucas test relies on the fact that the multiplicative order of a number a modulo n is n − 1 for a prime n when a is a primitive root modulo n. If we can show a is primitive for n, we can show n is prime.

## References

1. ^ a b Riesel (1994) pp.2-3
2. ^
3. Carl Pomerance; John L. Selfridge; Samuel S. Wagstaff, Jr. (July 1980). "The pseudoprimes to 25·109" (PDF). Mathematics of Computation. 35 (151): 1003–1026. doi:10.1090/S0025-5718-1980-0572872-7.
4. ^ Robert Baillie; Samuel S. Wagstaff, Jr. (October 1980). "Lucas Pseudoprimes" (PDF). Mathematics of Computation. 35 (152): 1391–1417. doi:10.1090/S0025-5718-1980-0583518-6. MR 0583518.
5. ^ Robert Baillie; Andrew Fiori; Samuel S. Wagstaff, Jr. (July 2021). "Strengthening the Baillie-PSW Primality Test". Mathematics of Computation. 90 (330): 1931–1955. arXiv:2006.14425. doi:10.1090/mcom/3616. S2CID 220055722.
6. ^ a b Adleman, Leonard M.; Huang, Ming-Deh (1992). Primality testing and Abelian varieties over finite field. Lecture notes in mathematics. Vol. 1512. Springer-Verlag. ISBN 3-540-55308-8.
7. ^ Chau, H. F.; Lo, H.-K. (1995). "Primality Test Via Quantum Factorization". arXiv:quant-ph/9508005.
8. ^ Pocklington, H. C. (1914). "The determination of the prime or composite nature of large numbers by Fermat's theorem". Cambr. Phil. Soc. Proc. 18: 29–30. JFM 45.1250.02.
9. ^
10. ^
11. ^ a b Agrawal, Manindra; Kayal, Neeraj; Saxena, Nitin (2004). "Primes is in P" (PDF). Annals of Mathematics. 160 (2): 781–793. doi:10.4007/annals.2004.160.781.
12. ^ Agrawal, Manindra; Kayal, Neeraj; Saxena, Nitin (2004). "PRIMES is in P" (PDF). Annals of Mathematics. 160 (2): 781–793. doi:10.4007/annals.2004.160.781.
13. ^ Carl Pomerance & Hendrik W. Lenstra (July 20, 2005). "Primality testing with Gaussian periods" (PDF).
14. ^ Popovych, Roman (December 30, 2008). "A note on Agrawal conjecture" (PDF).
15. ^ E. Allender, M. Saks, and I.E. Shparlinski, A lower bound for primality, J. Comp. Syst. Sci. 62 (2001), pp. 356–366.