TOPICS
Search

Prime Formulas


Prime formulas either produce the nth prime number as a function of n or take only prime values on a specified domain. Many require extremely accurate knowledge of a constant or encode inefficient primality tests (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 P), there exists a constant theta such that

 f(n)=|_theta^(3^n)_|,
(1)

where |_x_| is the floor function, is prime for all n>=1 (Ribenboim 1996, p. 186). The least such theta is Mills' constant, which is irrational (Saito 2025). Assuming the Riemann hypothesis, it begins theta=1.3063... (OEIS A051021), and the first few values of f(n) are 2, 11, 1361, 2521008887, 16022236204009818131831320183, ... (OEIS A051254; Caldwell and Cheng 2005, p. 5). There also exists a constant omega approx 1.9287822 (OEIS A086238) such that

 g(n)=|_2^(2^(·^(·^(·^2))))_()_(omega)^(AdjustmentBox[n, BoxBaselineShift -> -1])_|
(2)

(Wright 1951; Ribenboim 1996, p. 186) is prime for every n>=1. The first few values of g(n) are 3, 13, 16381, ... (OEIS A016104). Both formulas produce rapidly growing numbers and require increasingly precise values of theta or omega. Caldwell and Cheng (2005, pp. 5-6) computed the first ten terms of the Mills sequence, determining its constant to 6850 decimal places under the Riemann hypothesis.

Explicit formulas exist for the nth prime both as a function of n and in terms of the primes 2, ..., p_(n-1) (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.

A prime-generating formula sometimes known as Willans' formula can be constructed as follows. Let

F(j)=|_cos^2[pi((j-1)!+1)/j]_|
(3)
={1 for j=1 or j prime; 0 otherwise
(4)

for a positive integer j, where |_x_| is again the floor function. This formula is a consequence of Wilson's theorem and conceals the prime numbers j as those for which F(j)=1, i.e., the values of F(j) are 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, ... (OEIS A080339). Then

 pi(x)=-1+sum_(k=1)^(|_x_|)F(k)
(5)

for x>=1, and

p_n=1+sum_(m=1)^(2^n)|_|_n/(sum_(j=1)^(m)F(j))_|^(1/n)_|
(6)
=1+sum_(m=1)^(2^n)|_|_n/(1+pi(m))_|^(1/n)_|
(7)

for a positive integer n, where pi(m) is the prime counting function (Willans 1964; Havil 2003, pp. 168-169).

Gandhi gave the formula in which p_(n+1) is the unique integer such that

 1<2^(p_(n+1))(sum_(d|p_n#)(mu(d))/(2^d-1)-1/2)<2,
(8)

where p_n# is the primorial function (Gandhi 1971, Eynden 1972, Golomb 1974) and mu(n) is the Möbius function. It is also true that

 p_(n+1)=p_n+1+sum_(k=1)^(p_n-1)product_(j=1)^k[1-F(p_n+j)]
(9)

for n>=1. Each product is 1 until the first prime number after p_n is reached and is 0 thereafter. Bertrand's postulate ensures that this prime number occurs before 2p_n. In Willans' formula, the sum contains 2^n terms, so it is not a practical method of finding the nth prime number.

Hardy and Wright (1979, p. 414) give the formula

 p_n=1+sum_(j=1)^(2^n)f(n,pi(j))
(10)

for n>3, where

 f(x,y)={0   for x=y; 1/2[1+(x-y)/(|x-y|)]   for x!=y
(11)

and an "elementary" formula for the prime counting function is given by

 pi(n)=-1+sum_(j=3)^n[(j-2)!-j|_((j-2)!)/j_|]
(12)

for an integer n>=4, where |_x_| is the floor function.

A double sum for the nth prime p_n is

 p_n=1+sum_(k=1)^(2(|_nlnn_|+1))[1-|_(sum_(j=2)^(k)1+|_s(j)_|)/n_|],
(13)

where

 s(j)=-(sum_(s=1)^(j)(|_j/s_|-|_(j-1)/s_|)-2)/j
(14)

(Ruiz 2000).

Ruiz and Sondow (2014, pp. 96-97) give the simpler form

 p_n=2+sum_(k=2)^(|_2nlnn+2_|)(1-|_(pi(k))/n_|)
(15)

for an integer n>1, where pi(k) is the prime counting function. Their proof uses inequalities of Rosser and Schoenfeld to establish p_n<2nlnn+2<p_(2n). Within the summation range, the summand is 1 for k<p_n and 0 for k>=p_n, so the sum is p_n-2.

PrimeFormulaCipolla

An asymptotic formula for p_n is given by

 p_n∼n(lnn+lnlnn-1+(lnlnn-2)/(lnn)-(ln^2(lnn)-6lnlnn+11)/(2ln^2n)+...)
(16)

(Cipolla 1902). This asymptotic expansion is the inverse of the logarithmic integral Li(x) obtained by series reversion, where the inversion of Li(x) gives p_n because the prime number theorem says that Li(x)∼pi(x), where pi(x) is the prime counting function and the inverse of pi(x) is p_n in the sense that pi(p_n)=n. However, the formula oscillates a great deal as illustrated above, where p_n-p^^_n is the difference between the actual nth prime and that given by the Cipolla formula. Interestingly, truncating at n(lnn+lnlnn-1) gives the refined form of Rosser's theorem, which is a strict inequality on p_n. Salvy (1994) deals with more general cases.

PrimeFormulaBredihin

B. M. Bredihin proved that

 f(x,y)=x^2+y^2+1
(17)

takes prime values for infinitely many pairs of positive integers (x,y) (Honsberger 1976, p. 30). For example, f(1,1)=3, f(1,3)=11, f(1,9)=83, 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 x and y for which f(x,y) 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

 f(x,y)=1/2(y-1)[|B^2(x,y)-1|-(B^2(x,y)-1)]+2,
(18)

where

 B(x,y)=x(y+1)-(y!+1),
(19)

y! is the factorial, and x and y are positive integers (Honsberger 1976, p. 33). Whenever B(x,y)!=0, it has B(x,y)^2>=1 and yields the value 2. The remaining case is x=[(p-1)!+1]/p and y=p-1, in which case it simplifies to

 f(((p-1)!+1)/p,p-1)=p.
(20)

The formula therefore generates odd primes exactly once each (Honsberger 1976, p. 33) at the values for which the Wilson quotient x=[(p-1)!+1]/p is an integer, i.e., 1, 1, 5, 103, 329891, 36846277, 1230752346353, ... (OEIS A007619).

Bouras (2022) introduced a sequence taking only the value 1 or a prime number. Cloitre (2025, pp. 1-4) proved that it has the simpler representation

 a(n)=(n^2-n-1)/(gcd(n^2-n-1,n!))
(21)

for an integer n>=2, where gcd is the greatest common divisor and n! is a factorial. Its first terms are 1, 5, 11, 19, 29, 41, 11, 71, 89, 109, 131, 31, 181, ... (OEIS A356247). The prime numbers that occur are exactly 5 and those ending in 1 or 9 in decimal notation. The value 5 occurs once, at n=3, and every other prime number in the sequence occurs twice.

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.


See also

FRACTRAN, Mills' Constant, Prime Counting Function, Prime Number, Prime Products, Prime Sums, Rosser's Theorem, Sieve, Willans' Formula

Related Wolfram sites

https://functions.wolfram.com/NumberTheoryFunctions/Prime/

Explore with Wolfram|Alpha

References

Bouras, M. "A New Sequence of Prime Numbers." Romanian Math. Mag. Aug. 15, 2022. https://www.ssmrmh.ro/wp-content/uploads/2022/08/A-NEW-SEQUENCE-OF-PRIME-NUMBERS.pdf.Caldwell, C. K. and Cheng, Y. "Determining Mills' Constant and a Note on Honaker's Problem." J. Integer Sequences 8, Article 05.4.1, 1-9, 2005. https://cs.uwaterloo.ca/journals/JIS/VOL8/Caldwell/caldwell78.html.Cipolla, M. "La determinazione assintotica dell'n^(imo) numero primo." Napoli Rend. 3, 132-166, 1902.Cloitre, B. "On the Sequence A356247." Aug. 20, 2025. https://oeis.org/A356247/a356247_1.pdf.Conway, J. H. and Guy, R. K. The Book of Numbers. New York: Springer-Verlag, pp. 130 and 147, 1996.Dudley, U. "History of Formula for Primes." Amer. Math. Monthly 76, 23-28, 1969.Dusart, P. "The k^(th) Prime Is Greater than k(lnk+lnlnk-1) for k>=2." Math. Comput. 68, 411-415, 1999.Eynden, C. V. "A Proof of Gandhi's Formula for the nth Prime." Amer. Math. Monthly 79, 625, 1972.Finch, S. R. "Hardy-Littlewood Constants." §2.1 in Mathematical Constants. Cambridge, England: Cambridge University Press, pp. 84-94, 2003.Gandhi, J. M. "Formulae for the Nth Prime." Proc. Washington State University Conferences on Number Theory. pp. 96-107, 1971.Gardner, M. "Patterns and Primes." Ch. 9 in The Sixth Book of Mathematical Games from Scientific American. Chicago, IL: University of Chicago Press, pp. 79-90, 1984.Golomb, S. W. "A Direct Interpretation of Gandhi's Formula." Amer. Math. Monthly 81, 752-754, 1974.Guy, R. K. "Conway's Prime Producing Machine." Math. Mag. 56, 26-33, 1983.Guy, R. K. "Prime Numbers," "Formulas for Primes," and "Products Taken over Primes." Ch. A, §A17, and §B48 in Unsolved Problems in Number Theory, 2nd ed. New York: Springer-Verlag, pp. 3-43, 36-41, and 102-103, 1994.Hardy, G. H. and Wright, E. M. "Prime Numbers" and "The Sequence of Primes." §1.2 and 1.4 in An Introduction to the Theory of Numbers, 5th ed. Oxford, England: Clarendon Press, pp. 1-4, 5-6, 344-345, and 414, 1979.Havil, J. Gamma: Exploring Euler's Constant. Princeton, NJ: Princeton University Press, 2003.Honsberger, R. Mathematical Gems II. Washington, DC: Math. Assoc. Amer., 1976.Mills, W. H. "A Prime-Representing Function." Bull. Amer. Math. Soc. 53, 604, 1947.Mitrinović, D. S. and Sándor, J. Handbook of Number Theory. Dordrecht, Netherlands: Kluwer, 1995.Ribenboim, P. The New Book of Prime Number Records. New York: Springer-Verlag, 1996.Ruiz, S. M. "The General Term of the Prime Number Sequence and the Smarandache Prime Function." Smarandache Notions J. 11, 59-61, 2000.Ruiz, S. M. and Sondow, J. "Formulas for pi(x) and the nth Prime." Int. J. Math. Comput. Sci. 9, 95-98, 2014. https://future-in-tech.net/9.2/R-RuizSondow.pdf.Saito, K. "Mills' Constant Is Irrational." Mathematika 71, Article e70027, 2025. https://doi.org/10.1112/mtk.70027.Salvy, B. "Fast Computation of Some Asymptotic Functional Inverses." J. Symb. Comput. 17, 227-236, 1994.Sloane, N. J. A. Sequences A007619/M4023, A016104, A051021, A051254, A079544, A080339, A086238, and A356247 in "The On-Line Encyclopedia of Integer Sequences."Willans, C. P. "A Formula for the nth Prime Number." Math. Gaz. 48, 413-415, 1964.Wright, E. M. "A Prime-Representing Function." Amer. Math. Monthly 58, 616-618, 1951.

Referenced on Wolfram|Alpha

Prime Formulas

Cite this as:

Weisstein, Eric W. "Prime Formulas." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/PrimeFormulas.html

Subject classifications