Is there a function which will return the approximate value of the n th prime? I think this would be something like an approximate inverse prime counting function. For example, if I gave this function 25 it would return a number around 100, or if I gave this function 1000 it would return a number around 8000. I don't care if the number returned is prime or not, but I do want it to be fast (so no generating the first n prime numbers to return the n th.)
I would like this so that I can generate the first n prime numbers using a sieve (Eratosthenes or Atkin). Therefore, the approximation for n th the would ideally never underestimate the value of the actual n th prime.
(Update: see my answer for a good method of finding the upper bound of the n th prime number.)
My Best Prime(n) Estimate
Here's my most recent more experimental formula. btw. The ten trillionth prime is
323,780,508,946,331
this formula works quite well at that scale not sure if it continues to get closer thann*ln(n)+n*(ln(ln(n))-0.9385)
.To complement Dana J's Upper bound this formula should give you a good lower bound.
Thanks for all of those answers. I suspected there was something fairly simple like that, but I couldn't find it at the time. I've done a bit more research too.
As I want it for a sieve to generate the first n prime numbers, I want the approximation to be greater than or equal to the nth prime. (Therefore, I want the upper bound of the nth prime number.)
Wikipedia gives the following upper bound for
n >= 6
where
p_n
is the nth prime, andlog
is the natural logarithm. This is a good start, but it can overestimate by a not inconsiderable amount. This article in The College Mathematics Journal gives a tighter upper bound forn >= 7022
This is a much tighter bound as the following table shows
I implemented my nth prime approximation function to use the second approximation for
n >= 7022
, the first approximation for6 <= n < 7022
, and an array lookup for the first 5 prime numbers.(Although the first method isn't a very tight bound, especially for the range where I use it, I am not concerned as I want this for a sieve, and a sieve of smaller numbers is computationally cheap.)
Tighter bounds:
These should not ever be less than the actual nth_prime, should work for any 64-bit input, and be an order of magnitude or more closer than the formula from Robin given earlier (or Wimblik's complicated range-limited formula). For my use I have a slightly larger small primes table so can tighten up the last estimate a bit more. Technically from the formulas we could use floor() instead of ceil() but I worry about precision.
Edit: Another option for improving this a bit is implementing good prime count bounds (e.g. Axler 2014) and doing a binary search on them. My code for this method takes ~10x longer than the above (still running in under a millisecond), but can reduce the error percentage by an order of magnitude.
If you want an estimate for the nth prime, you can do:
Lastly, if you have a very fast prime count method such as one of the LMO implementations (there are three open source implementations now), you can write a fast precise nth_prime method. Computing the 10^10th prime can be done in a few milliseconds, and the 10^13th in a couple seconds (on a modern fast machine). The approximations are extremely fast at all sizes and work for far larger numbers, but everyone has a different idea of what "large" means.
An efficient implementation is probably not possible with a sieve. Think what would happen if you want to have the first 10.000 prime numbers. You probably would have to make a sieve over a huge bigger amount of numbers.
Your own implentation in this question and my answer are good ways to implement this without knowing the appr. value of a prime
Prime number theorem gives a number of primes below a threshold value, so it could be used to give an approximate value for the nth prime.