There exist a variety of formulas for either producing the th prime as a function of or taking on only prime values. However, all such formulas require either extremely accurate knowledge of some unknown constant, or effectively require knowledge of the primes ahead of time in order to use the formula (Dudley 1969; Ribenboim 1996, p. 186). There also exist simple prime-generating polynomials that generate only primes for the first (possibly large) number of integer values.
There are also many beautiful formulas involving prime sums and prime products that can be done in closed form.
Considering examples of formulas that produce only prime numbers (although not necessarily the complete set of prime numbers ), there exists a constant (OEIS A051021) known as Mills' constant such that
where is the floor function, is prime for all (Ribenboim 1996, p. 186). The first few values of are 2, 11, 1361, 2521008887, ... (OEIS A051254). It is not known if is irrational. There also exists a constant (OEIS A086238) such that
(Wright 1951; Ribenboim 1996, p. 186) is prime for every . The first few values of are 3, 13, 16381, ... (OEIS A016104). In the case of both and , the numbers at grow so rapidly that an extremely precise value of or is needed in order to obtain the correct value, and values for are effectively incomputable.
Explicit formulas exist for the th prime both as a function of and in terms of the primes 2, ..., (Hardy and Wright 1979, pp. 5-6, 344-345, and 414; Guy 1994, pp. 36-41), a number of which are given below. However, it should again be emphasized that these formulas are extremely inefficient, and in many (if not all) cases, simply performing an efficient sieving would yield the primes much more quickly and efficiently.
for an integer, where is again the floor function. This formula is a consequence of Wilson's theorem and conceals the prime numbers as those for which , i.e., the values of are 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, ... (OEIS A080339). Then
where is the prime counting function (Willans 1964; Havil 2003, pp. 168-169).
Gandhi gave the formula in which is the unique integer such that
where is the primorial function (Gandhi 1971, Eynden 1972, Golomb 1974) and is the Möbius function. It is also true that
(Ribenboim 1996, pp. 180-182). Note that the number of terms in the summation to obtain the th prime is , so these formulas turn out not to be practical in the study of primes.
Hardy and Wright (1979, p. 414) give the formula
for , where
and an "elementary" formula for the primecounting function is given by
(correcting a sign error), where is the floor function.
A double sum for the th prime is
An asymptotic formula for is given by
(Cipolla 1902). This asymptotic expansion is the inverse of the logarithmic integral obtained by series reversion, where the inversion of gives because the prime number theorem says that , where is the prime counting function and the inverse of is in the sense that . However, the formula oscillates a great deal as illustrated above, where is the difference between the actual th prime and that given by the Cipolla formula. Interestingly, truncating at gives the refined form of Rosser's theorem, which is a strict inequality on . Salvy (1994) deals with more general cases.
B. M. Bredihin proved that
takes prime values for infinitely many integral pairs (Honsberger 1976, p. 30). For example, , , , and so on. The primes of this form are 3, 11, 19, 41, 53, 59, 73, 83, 101, 107, 131, 137, 149, 163, ... (OEIS A079544; Mitrinović and Sándor 1995, p. 11). The values of and for which is prime are plotted above, showing some interesting patterns.
It is in general not difficult to artificially construct formulas that always generate prime numbers. For example, consider the formula
is the factorial, and and are positive integers (Honsberger 1976, p. 33). This will always have and hence yield the value 2 unless and , in which case it simplifies to
The formula therefore generates odd primes exactly once each (Honsberger 1976, p. 33) at the values for which the Wilson quotient is an integer, i.e., 1, 1, 5, 103, 329891, 36846277, 1230752346353, ... (OEIS A007619).
The FRACTRAN game (Guy 1983, Conway and Guy 1996, p. 147) provides an unexpected means of generating the prime numbers based on 14 fractions, but it is actually just a concealed version of a sieve.