In number theory, a formula for primes is a formula that outputs prime numbers. Such formulas for calculating primes do exist; however, they are computationally very slow, compared to a simple algorithm for prime-finding. A number of constraints are known, showing what such a "formula" can and cannot be.
Formulas based on Wilson's theorem A simple formula that produces all primes, albeit mostly interspersed by the prime number 2, is
f(n)=⌊n!mod(n+1)n⌋(n−1)+2 for positive integer n, where ⌊ ⌋ is the floor function, which rounds down to the nearest integer. The first few values of the function are 2, 2, 3, 2, 5, 2, 7, 2, 2, 2, 11... The formula works because by Wilson's theorem, n+1 is prime if and only if n!≡n(modn+1). Thus, when n+1 is prime, the first factor in the product becomes one, and the formula produces the prime number n+1. But when n+1 is not prime, the first factor becomes zero and the formula produces the prime number 2. This formula is not an efficient way to generate prime numbers because evaluating n!mod(n+1) requires about n−1 multiplications and reductions modulo n+1. In 1964, Willans gave the formula
pn=1+∑i=12n⌊(n∑j=1i⌊(cos(j−1)!+1jπ)2⌋)1/n⌋ for the nth prime number pn. This formula reduces to
pn=1+∑i=12n[π(i)<n]; that is, it tautologically defines pn as the smallest integer m for which the prime-counting function π(m) is at least n. This formula is also not efficient. In addition to the appearance of (j−1)!, it computes pn by adding up pn copies of 1; for example,
p5=1+1+1+1+1+1+1+1+1+1+1+0+0+⋯+0=11. The articles What is an Answer? by Herbert Wilf (1982) and Formulas for Primes by Underwood Dudley (1983) have further discussion about the worthlessness of such formulas. A shorter formula based on Wilson's theorem was given by J. P. Jones in 1975, using mod as a function:
pn=∑i=0n2(1−˙((∑j=0i(j−˙1)!2modj)−˙n)). Here, −˙ is the monus operator, defined as a−˙b=max(a−b,0), and xmod0 is defined to be x.
Recurrence relations for primes
Gandhi's formula In 1971, J. M. Gandhi proved that pn=⌊1−log2(sn−1−12)⌋,where sn=∑d|pn#μ(d)2d−1, μ is the Möbius function and d runs through all divisors of pn#, the primorial of pn. This formula should be seen as a recurrence relation for the prime numbers, expressing pn in terms of p1,p2,…,pn−1. This expression for sn given by Gandhi results from an application of a modified Sieve of Eratosthenes operating on the exponents of the powers of 12 in the sum ∑k=1∞12k after n steps. More precisely, Gandhi showed that sn=121+12pn+1+…, where the dots represent terms with increasing exponents greater than pn+1. There are analog recurrences where the process is done in a base b other than 2. In 2025, a simpler expression for sn was published: sn={12pn#−1∏i=1n2pn#−2pn#/pi2pn#/pi−1}, where {x} denotes the fractional part of x. It is based on a more clever use of the Sieve of Eratosthenes through the Chinese remainder theorem.
Golomb's formula Inspired by Gandhi's proof, Golomb proved the following recurrencepn=lims→∞(ζ(s)∏k=1n−1(1−pk−s)−1)−1s,where ζ denotes the Riemann zeta function. It is based on the Euler product for ζ.
Prime-representing constants The notion of continued fraction can be used to define the constant u1=[p1,p2,p3,...]=2.31303673643... (sequence A064442 in the OEIS) from which we can recover the prime number sequence using the following recurrence relationship un+1=(un−⌊un⌋)−1, and it follows that pn=⌊un⌋. An alternative construction was given by Fridman et al.. Given the constant f1=2.920050977316… (sequence A249270 in the OEIS), for n≥2, define the sequence fn=⌊fn−1⌋(fn−1−⌊fn−1⌋+1) where ⌊ ⌋ is the floor function. Then for n≥1, pn=⌊fn⌋. The initial constant f1=2.920050977316 given in the article is precise enough for equation (1) to generate the primes through 37, the twelfth prime. The exact value of f1 that generates all primes is given by the rapidly-converging series
f1=∑n=1∞pn−1pn−1#=2−11+3−12+5−12⋅3+7−12⋅3⋅5+⋯, The more digits of f1 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:
f1≃2.920050977316134712092562917112019. This has enough digits for equation (1) to yield again the 25 primes less than 100.
Mills' formula The first such formula known was established by W. H. Mills (1947), who proved that there exists a real number A such that, if
dn=A3n then
⌊dn⌋=⌊A3n⌋ is a prime number for all positive integers n. 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. This value gives rise to the primes ⌊d1⌋=2, ⌊d2⌋=11, ⌊d3⌋=1361, ... (sequence A051254 in the OEIS). Very little is known about the constant A. This formula has no practical value, because there is no known way of calculating the constant without finding primes in the first place. There is nothing special about the floor function in the formula. Tóth proved that there also exists a constant B such that
⌈Brn⌉ is also prime-representing for r>2.106…. In the case r=3, the value of the constant B begins with 1.24055470525201424067... The first few primes generated are:
2,7,337,38272739,56062005704198360319209, 176199995814327287356671209104585864397055039072110696028654438846269,… Without assuming the Riemann hypothesis, Elsholtz developed several prime-representing functions similar to those of Mills. For example, if A=1.00536773279814724017…, then ⌊A1010n⌋ is prime for all positive integers n. Similarly, if A=3.8249998073439146171615551375…, then ⌊A313n⌋ is prime for all positive integers n.
Wright's formula A tetrationally growing prime-generating formula similar to Mills' comes from a theorem of E. M. Wright. He proved that there exists a real number α such that, if
g0=α and gn+1=2gn for n≥0, then
⌊gn⌋=⌊2…22α⌋ is prime for all n≥1. Wright gives the first seven decimal places of such a constant: α=1.9287800. This value gives rise to the primes ⌊g1⌋=⌊2α⌋=3, ⌊g2⌋=13, and ⌊g3⌋=16381. ⌊g4⌋ is even, and so is not prime. However, with α=1.9287800+8.2843⋅10−4933, ⌊g1⌋, ⌊g2⌋, and ⌊g3⌋ are unchanged, while ⌊g4⌋ is a prime with 4932 digits. This sequence of primes cannot be extended beyond ⌊g4⌋ without knowing more digits of α. Like Mills' formula, and for the same reasons, Wright's formula cannot be used to find primes.
… excerpt ends here. Continue reading the full article.