In number theory, a formula for primes is a formula generating the prime numbers, exactly and without exception. No such formula which is efficiently computable is known.^{[clarification needed]} A number of constraints are known, showing what such a "formula" can and cannot be.
A simple formula is
for positive integer , where is the floor function, which rounds down to the nearest integer. By Wilson's theorem, is prime if and only if . Thus, when is prime, the first factor in the product becomes one, and the formula produces the prime number . But when is not prime, the first factor becomes zero and the formula produces the prime number 2.^{[1]} This formula is not an efficient way to generate prime numbers because evaluating requires about multiplications and reductions modulo .
In 1964, Willans gave the formula
for the th prime number .^{[2]} This formula is also not efficient. In addition to the appearance of , it computes by adding up copies of ; for example, .
Because the set of primes is a computably enumerable set, by Matiyasevich's theorem, it can be obtained from a system of Diophantine equations. Jones et al. (1976) found an explicit set of 14 Diophantine equations in 26 variables, such that a given number k + 2 is prime if and only if that system has a solution in nonnegative integers:^{[3]}
The 14 equations α_{0}, …, α_{13} can be used to produce a primegenerating polynomial inequality in 26 variables:
i.e.:
is a polynomial inequality in 26 variables, and the set of prime numbers is identical to the set of positive values taken on by the lefthand side as the variables a, b, …, z range over the nonnegative integers.
A general theorem of Matiyasevich says that if a set is defined by a system of Diophantine equations, it can also be defined by a system of Diophantine equations in only 9 variables.^{[4]} Hence, there is a primegenerating polynomial as above with only 10 variables. However, its degree is large (in the order of 10^{45}). On the other hand, there also exists such a set of equations of degree only 4, but in 58 variables.^{[5]}
The first such formula known was established by W. H. Mills (1947), who proved that there exists a real number A such that, if
then
is a prime number for all positive integers n.^{[6]} If the Riemann hypothesis is true, then the smallest such A has a value of around 1.3063778838630806904686144926... (sequence A051021 in the OEIS) and is known as Mills' constant.^{[7]} This value gives rise to the primes , , , ... (sequence A051254 in the OEIS). Very little is known about the constant A (not even whether it is rational). This formula has no practical value, because there is no known way of calculating the constant without finding primes in the first place.
Note that there is nothing special about the floor function in the formula. Tóth^{[8]} proved that there also exists a constant such that
is also primerepresenting for (Tóth 2017).
In the case , the value of the constant begins with 1.24055470525201424067... The first few primes generated are:
Without assuming the Riemann hypothesis, Elsholtz developed several primerepresenting functions similar to those of Mills. For example, if , then is prime for all positive integers . Similarly, if , then is prime for all positive integers .^{[9]}
Another primegenerating formula similar to Mills' comes from a theorem of E. M. Wright. He proved that there exists a real number α such that, if
then
is prime for all .^{[10]} Wright gives the first seven decimal places of such a constant: . This value gives rise to the primes , , and . is even, and so is not prime. However, with , , , and are unchanged, while is a prime with 4932 digits.^{[11]} This sequence of primes cannot be extended beyond without knowing more digits of . Like Mills' formula, and for the same reasons, Wright's formula cannot be used to find primes.
Given the constant (sequence A249270 in the OEIS), for , define the sequence

(1) 
where is the floor function. Then for , equals the th prime: , , , etc. ^{[12]} The initial constant given in the article is precise enough for equation (1) to generate the primes through 37, the th prime.
The exact value of that generates all primes is given by the rapidlyconverging series
where is the th prime, and is the product of all primes less than . The more digits of that we know, the more primes equation (1) will generate. For example, we can use 25 terms in the series, using the 25 primes less than 100, to calculate the following more precise approximation:
This has enough digits for equation (1) to yield again the 25 primes less than 100.
As with Mills' formula and Wright's formula above, in order to generate a longer list of primes, we need to start by knowing more digits of the initial constant, , which in this case requires a longer list of primes in its calculation.
In 2018 Simon Plouffe conjectured a set of formulas for primes. Similarly to the formula of Mills, they are of the form
where is the function rounding to the nearest integer. For example, with and , this gives 113, 367, 1607, 10177, 102217... Using and with a certain number between 0 and one half, Plouffe found that he could generate a sequence of 50 probable primes (with high probability of being prime). Presumably there exists an ε such that this formula will give an infinite sequence of actual prime numbers. The number of digits starts at 501 and increases by about 1% each time.^{[13]}^{[14]}
It is known that no nonconstant polynomial function P(n) with integer coefficients exists that evaluates to a prime number for all integers n. The proof is as follows: suppose that such a polynomial existed. Then P(1) would evaluate to a prime p, so . But for any integer k, also, so cannot also be prime (as it would be divisible by p) unless it were p itself. But the only way for all k is if the polynomial function is constant. The same reasoning shows an even stronger result: no nonconstant polynomial function P(n) exists that evaluates to a prime number for almost all integers n.
Euler first noticed (in 1772) that the quadratic polynomial
is prime for the 40 integers n = 0, 1, 2, ..., 39, with corresponding primes 41, 43, 47, 53, 61, 71, ..., 1601. The differences between the terms are 2, 4, 6, 8, 10... For n = 40, it produces a square number, 1681, which is equal to 41 × 41, the smallest composite number for this formula for n ≥ 0. If 41 divides n, it divides P(n) too. Furthermore, since P(n) can be written as n(n + 1) + 41, if 41 divides n + 1 instead, it also divides P(n). The phenomenon is related to the Ulam spiral, which is also implicitly quadratic, and the class number; this polynomial is related to the Heegner number . There are analogous polynomials for (the lucky numbers of Euler), corresponding to other Heegner numbers.
Given a positive integer S, there may be infinitely many c such that the expression n^{2} + n + c is always coprime to S. The integer c may be negative, in which case there is a delay before primes are produced.
It is known, based on Dirichlet's theorem on arithmetic progressions, that linear polynomial functions produce infinitely many primes as long as a and b are relatively prime (though no such function will assume prime values for all values of n). Moreover, the Green–Tao theorem says that for any k there exists a pair of a and b, with the property that is prime for any n from 0 through k − 1. However, as of 2020,^{[update]} the best known result of such type is for k = 27:
is prime for all n from 0 through 26.^{[15]} It is not even known whether there exists a univariate polynomial of degree at least 2, that assumes an infinite number of values that are prime; see Bunyakovsky conjecture.
Another prime generator is defined by the recurrence relation
where gcd(x, y) denotes the greatest common divisor of x and y. The sequence of differences a_{n+1} − a_{n} starts with 1, 1, 1, 5, 3, 1, 1, 1, 1, 11, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 23, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 47, 3, 1, 5, 3, ... (sequence A132199 in the OEIS). Rowland (2008) proved that this sequence contains only ones and prime numbers. However, it does not contain all the prime numbers, since the terms gcd(n + 1, a_{n}) are always odd and so never equal to 2. 587 is the smallest prime (other than 2) not appearing in the first 10,000 outcomes that are different from 1. Nevertheless, in the same paper it was conjectured to contain all odd primes, even though it is rather inefficient.^{[16]}
Note that there is a trivial program that enumerates all and only the prime numbers, as well as more efficient ones, so such recurrence relations are more a matter of curiosity than of any practical use.