I've solved for it making a computer program, but was wondering there was a mathematical equation that you could use to solve for the nth prime?
5 Answers
No, there is no known formula that gives the nth prime, except artificial ones you can write that are basically equivalent to "the $n$th prime". But if you only want an approximation, the $n$th prime is roughly around $n \ln n$ (or more precisely, near the number $m$ such that $m/\ln m = n$) by the prime number theorem. In fact, we have the following asymptotic bound on the $n$th prime $p_n$:
$n \ln n + n(\ln\ln n - 1) < p_n < n \ln n + n \ln \ln n$ for $n\ge{}6$
You can sieve within this range if you want the $n$th prime. [Edit: Using more accurate estimates you'll have a much smaller range to sieve; see the answer by Charles.]
Entirely unrelated: if you want to see formulae that generate a lot of primes (not the $n$th prime) up to some extent, like the famous $f(n)=n^2-n+41$, look at the Wikipedia article formula for primes, or Mathworld for Prime Formulas.

- 41,374
-
2It should be noted that this is derived from the prime number theorem. – BlueRaja - Danny Pflughoeft Jul 30 '10 at 20:40
-
Right. I had omitted mentioning it, but thanks to your reminder I went and found a precise bound I hadn't previously seen. :-) – ShreevatsaR Jul 30 '10 at 20:58
-
I don't know why the downvote, but a closely related question was just posted (http://math.stackexchange.com/questions/940338/the-myth-of-no-prime-formula) and perhaps it has gotten this topic some new attention. (I think your first sentence is a good summary conclusion, but someone else may disagree.) – David K Sep 22 '14 at 20:34
-
1How about Mill's formula? https://en.wikipedia.org/wiki/Mills%27_constant – Ovi May 27 '16 at 21:25
-
@Ovi While interesting, that doesn't give the $n$th prime; it gives only a particular subsequence of primes (where moreover knowing anything about the constant or even the first generated prime depends on the Riemann hypothesis, and even then it seems the constant is calculated from the primes, rather than the primes being generated from the constant: the value of the constant is thus a "summary" which encodes the primes found.) – ShreevatsaR May 28 '16 at 01:48
-
@ShreevatsaR Yeah unfortunately it doesn't give the $n^{th}$ prime, but I believe its existence has already been proved. The issue is that we don't really know how to calculate it, so the best we can do is to use the formula (given on the wiki page) $A \approx a(n)^{\frac {1}{3^n}}$. However if the Riemann Hypothesis is true, there is another way to calculate it. I'm not totally sure of this though, it is just my interpretation of Dr. James Grime's explanation of it in this Numberphile video https://www.youtube.com/watch?v=6ltrPVPEwfo – Ovi May 28 '16 at 03:10
-
The lower bound is due to Dusart (1999); from where does the upper bound come from? – Jose Brox Jan 12 '18 at 11:02
-
I have found it: the upper bound comes from Rosser (1941) – Jose Brox Jan 12 '18 at 18:40
-
@JoseBrox Great, was about to answer myself with what I know: the reference given on Wikipedia is Bach, Eric; Shallit, Jeffrey (1996). Algorithmic Number Theory. Foundations of Computing Series. 1. Cambridge: MIT Press. p. 233. ISBN 0-262-02405-5. MR 1406794 but of course the original source is probably older (what you found). – ShreevatsaR Jan 12 '18 at 18:46
-
@ShreevatsaR Thanks. I've updated the Wikipedia with a reference to Rosser's paper. – Jose Brox Jan 12 '18 at 19:10
-
1As for one of the artificial ones you mention: For any $c>2$ one can show there are infinitely many $A$ such that $p_n = \left\lfloor c^{2n-1}(c^{(n-1)^2}A - \lfloor c^{(n-1)^2}A\rfloor)\right\rfloor$. For example for $c=10$ we can choose $A = 0.200300005\ldots = \sum \frac{p_n}{10^{n^2}} $ (or this number plus any integer). However this is useless in practice as it requires complete knowledge of all the $n$ first primes in order to compute the $n$th prime. – Winther Jul 25 '18 at 13:28
-
There are explicit formulas for the nth prime, most of these formulas use Wilson's theorem. – Boots Aug 30 '18 at 02:44
-
@Boots I mentioned artificial ones you can write that are basically equivalent to "the nth prime" — the point is that using a formula should not take more work than simply computing the nth prime directly: such a formula is not worth talking about. – ShreevatsaR Aug 30 '18 at 04:23
-
@ShreevatsaR a definite formula is better (at least to me) than a sieve. – The Empty String Photographer Mar 31 '23 at 16:32
-
@PlaceReporter99 What do you mean by "a definite formula"? Unless such a formula let you compute (say) the 100th prime number with less computation than alternative methods (sieve, or even trial division), it is hard to see in what way it is “better”—it is just a obfuscated way of writing “the nth prime”. If you can carry out the computation more conveniently with a formula, then let me know. :-) – ShreevatsaR Apr 02 '23 at 14:20
-
@ShreevatsaR What I’m saying is that I prefer to get the *100% guaranteed* prime with a formula even if it’s slower than a sieve. You might have different preferences, and that’s ok. – The Empty String Photographer Apr 02 '23 at 16:02
-
@PlaceReporter99 You're 100% guaranteed to get a prime even with a sieve. Or trial division. Or any number of different methods. What is a formula according to you? (Note that any computation can be encoded into a formula, and in fact that's what the more artificial formulas do…) – ShreevatsaR Apr 02 '23 at 16:32
-
@ShreevatsaR take a look at my answer – The Empty String Photographer Apr 02 '23 at 16:36
-
@TheEmptyStringPhotographer As you say in your later edit to your answer, that formula (by C. P. Willans) is not useful for constructing a Taylor series or for any purpose really. I just linked from the answer a nice video about that formula; there are also good discussions about how such formulas are (clever, but) useless (“the disease was preferable to the cure”) in Herbert Wilf's “What is an Answer?” and Underwood Dudley's “Formulas for Primes”. – ShreevatsaR Sep 01 '23 at 16:58
Far better than sieving in the large range ShreevatsaR suggested (which, for the 10¹⁵th prime, has 10¹⁵ members and takes about 33 TB to store in compact form), take a good first guess like Riemann's R and use one of the advanced methods of computing pi(x) for that first guess. (If this is far off for some reason—it shouldn't be—estimate the distance to the proper point and calculate a new guess from there.) At this point, you can sieve the small distance, perhaps just 10⁸ or 10⁹, to the desired number.
This is about 100,000 times faster for numbers around the size I indicated. Even for numbers as small as 10 to 12 digits, this is faster if you don't have a precomputed table large enough to contain your answer.

- 163

- 32,122
-
-
3@ShreevatsaR: Don't get me wrong -- your answer is far better than the naive method of stepping through numbers and testing them! I was just suggesting an improvement. :) – Charles Sep 09 '10 at 18:55
-
"At this point you can sieve the small distance, perhaps just 10^8 or 10^9, to the desired number." How would you recognize this number? – Liviu Jan 17 '16 at 15:00
-
@Liviu: Suppose you want the millionth prime and you find an $x$ with 998,000 primes less than it. You just need to "count up" 2000 primes. The expected length needed is then about $2000\log x$, but you should probably add, say, 10% or so in case the primes don't cooperate. – Charles Jan 18 '16 at 09:47
-
@Charles I know the 1000000th prime because I know the 998000th prime because I know the 996000th prime ... because I know the 20th prime, that's 71. It seems easy now ... oh, wait a minute – Liviu Jan 21 '16 at 21:59
-
@Liviu: Use a $\pi(x)$ algorithm to get a good guess and sieve from there. If for some reason your guess is very far off, take a second guess and sieve from that point. If you are currently at the 20th prime you shouldn't start sieving yet. :p – Charles Jan 21 '16 at 23:46
-
4@Charles "your guess is very far off" ... how can I know that a prime is the prime I am searching for ... without counting all the primes until it? – Liviu Jan 22 '16 at 14:51
-
3@Liviu: Suppose you have a fast test for (1) whether a number is prime or not and (2) a fast test to find the number of primes less than or equal to a given number. If I claim that a number $p$ is the $n$th prime, all you have to do is check that $p$ is prime using (1) and check that $\pi(p)=n$ by (2). (1) is relatively easy, but (2) is hard. See http://mathworld.wolfram.com/PrimeCountingFunction.html for an overview on the latter. There are two classes of fast approaches to (2), analytic and combinatorial, and they are roughly tied at record sizes (at smaller sizes combinatorial is better). – Charles Jan 22 '16 at 22:45
There are formulas on Wikipedia, though they are messy. No polynomial $p(x)$ can output the $n$th prime for all $n$, as is explained in the first section of the article.
There is, however, a polynomial in 26 variables whose nonnegative values are precisely the primes. (This is fairly useless as far as computation is concerned.) This comes from the fact that the property of being a prime is decidable, and the theorem of Matiyasevich.

- 31,310
-
Note that this 26-variable polynomial isn't well suited to generate primes, as most of its codomain is negative. – stevenvh Aug 07 '10 at 17:39
-
1I remember it was presented to us in the elementary course of number theory, and I think the teacher said that after much work they managed to reduce it to 22 variables. – Asaf Karagila Sep 11 '10 at 17:59
-
15This 26-variable polynomial still gives me the creeps. I find it very strange indeed. – Sputnik Jun 07 '11 at 22:45
-
-
1@PlaceReporter99 It later dawned on me that its positive values are not precisely the primes as this answer stated -- they just happen to be prime. There is a big difference. It's no longer scary, but I'm still none the wiser as to how you would go about constructing such a ridiculous polynomial. The one with 42 variables in degree 5 is only a bit better. Still waiting for someone to produce a cubic in one variable that accomplishes this... Surely only a matter of time. – Sputnik Apr 03 '23 at 10:03
-
No such formula is known, but there are a few that give impressive results. A famous one is Euler's: $$P(n) = n^2 − n + 41$$ Which yields a prime for every natural number lower than $41$, though not necessarily the $n$th prime.
See more here.
According to Wikipedia, there is a formula for the $n^{\text{th}}$ prime, although it is very inefficient (adding up lots of copies of $1$s):
$$p_n=1+\sum_{i=1}^{2^n} \left\lfloor\left(\frac n{\sum_{j=1}^i \left\lfloor(\cos\frac{(j-1)!+1}{j}\pi)^2\right\rfloor}\right)^{1/n}\right\rfloor$$
However, this means it is possible to work out the derivative of $\not p_n$ w.r.t $\not n$ and construct a Taylor series.
Edit
After I asked Wolfram Alpha the derivative of $f(x)^{\frac1x}$, I found out it had a term of something to the power of $\frac1{x-1}$, which means you cannot construct a Taylor series as $\frac10$ is undefined.
Read the comment below for an explanation by ShreevatsaR.
-
This formula by C. P. Willans is from this paper, which explains the trick: (1) By Wilson's theorem, $((j-1)!+1)/j$ is fractional when $j$ is composite, so $\left\lfloor\left(\cos \frac{(j-1)! + 1}{j} \pi\right)^2\right\rfloor$ is just another way of writing “$j$ is not composite”, (2) $\lfloor \left(n/(1 + a)\right)^{1/n}\rfloor$ is the same as “$a \le n$”, so overall, the formula amounts to $p_n = 1 + \sum_{i} [i\text{ is prime and }\pi(i) \le n]$, which is just a convoluted way of writing “the $n$th prime” with more steps. – ShreevatsaR Apr 02 '23 at 16:55