ArticleslgStudy

mathematics

Formula for primes

Formula for primes is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Formula for primes rather than just read about it. In short: 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.

Key takeaways

  • Formula for primes belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Formula for primes to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Formula for primes from memory before moving on to harder problems.

Reference excerpt

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.

Worked examples

Example 1 — a first encounter with Formula for primes

Start with the simplest possible case. Write down what Formula for primes claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Formula for primes before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Formula for primes ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Formula for primes

In research
Formula for primes appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Formula for primes in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Formula for primes is common in secondary-school and first-year university syllabi. It links to neighbouring topics Formulas, Prime numbers, so understanding it makes those chapters shorter.
In everyday life
Look for Formula for primes outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Formula for primes” →

How to study Formula for primes in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Formula for primes means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Formula for primes out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Formula for primes in simple terms?

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.

Why does Formula for primes matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Formula for primes?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Formula for primes.

Tags

  • Formulas
  • Prime numbers

Keep exploring