4

The prime counting function $ \pi (x) \approx \dfrac {x} {\ln(x-1)} $. This function returns the number of primes less than $x$.

Note: $x-1$ gives a better estimate than $x$.

How to find $x$ given $ \pi(x) $ ?

lapin
  • 479
  • 5
    There are multiple $x$ which have the same value of $\pi(x)$ so there is really no hope of an inverse. You can get an interval for which $x$ would come from: if $\pi(x)=n$ then $p_{n}\leq x<p_{n+1}$. But maybe you mean given $\frac{x}{\ln(x-1)}$ how does one solve for $x$? – Eoin Jul 03 '15 at 15:04
  • 3
    As @Eoin said there is no an inverse for the counting function because it's not bijective, but we can find an asymptotic inverse, we know that $\pi(p_n)=n$ so we can say the easiest way: the inverse of $n\to \pi(n)$ is the n-th prime $n\to p_n$ which has the asymptotic formula $n\ln(n)$ – Elaqqad Jul 03 '15 at 15:09
  • I don't mean the exxact value of x, just an upper bound or an estimate, i.e. How far do I have to check to get $\pi(x)$ primes ? @Eoin. – lapin Jul 03 '15 at 15:10
  • 1
    And we know that if $n=\pi(x)$ then $$n(\ln n+\ln \ln n-1) \leq x\leq (n+1)(\ln (n+1) +\ln \ln (n+1))$$ – Elaqqad Jul 03 '15 at 15:13
  • 2
    A quite good approximation is $R^{-1}(x)$ (where R=Riemann R function, x = your $\pi(x)$, binary search can find the inverse). Easier but not as good is a truncated Cipolla (1902) series, e.g. m=2 with a 3rd order correction. Exact answers can be had using a good estimate followed by a fast prime count (e.g. LMO) followed by sieving the difference (which is small if your estimator is good). Good bounds are shown in Dusart 2010 and Axler 2013, or can be done with inverse prime count bounds (again Dusart 1999/2010 and Axler 2013/2014). – DanaJ Jul 03 '15 at 16:01
  • One might construe this question as "What is the inverse of $x\mapsto\dfrac x{\ln(x-1)}$. ${}\qquad{}$ – Michael Hardy Jul 03 '15 at 17:10
  • @MichaelHardy, yet the final sentence is "How to find x given π(x) ?" – DanaJ Jul 04 '15 at 03:12
  • Perhaps you mean how to find $p(\pi(x))$ where $p(n)$ is the $n^{th}$ prime? – Steven Clark Dec 19 '24 at 15:31

3 Answers3

1

There is of course not just one solution, since multiple integers n will have the same value $\pi(n)$. You could create a unique solution by asking for the n-th prime.

A good approximation for $\pi(n)$ is $\pi(n) = li(n) + O (1/n)$. $pi(n)$ behaves as if it was the sum of the logarithmic integral $li(n)$ which isn't hard to calculate, plus a pseudo-random component. For x that isn't too large, you use usual numerical methods to find an n with $li(n) \approx x$. The value $\pi(n)$ will be O(n) away from this.

So you use a method that calculates $pi(n)$ exactly, which can be done in $O(n^{2/3})$ steps, and which is close to but not equal to x. Then you determine an interval that ought to contain the n with $\pi(n) = x$ and use a sieve to calculate $\pi(n)$ for all n in this interval.

gnasher729
  • 10,611
  • 20
  • 38
1

If what you are looking for is bounds, you could use something like this.

Consider the following non-asymptotic bounds for $\pi(x)$, proven by Dusart in 2018 (holding for $x>5393$):

$$\frac{x}{\log(x)-1}<\pi(x)<\frac{x}{\log(x)-1.112}$$

To get the maximum value for $p(n)$, we take the lower bound with $x=p(n)$ :

$$\pi(p(n))=\frac{p(n)}{\log p(n)-1}$$ $$n\left(\log p(n)-1\right)-p(n)=0$$ $$p(n)=-nW_{-1}\left(\frac{-e^{1}}{n}\right)$$

$W_{k}(n)$ is the analytic continuation of the product log function. Now if we do the same with the upper bound, we get non-asymptotic bounds for the $N^{th}$ prime $p(n)$:

$$-nW_{-1}\left(\frac{-e^{1.112}}{n}\right)<p(n)<-nW_{-1}\left(\frac{-e}{n}\right)$$

Whith these bounds you know how far you would have to check to get $n$ primes, like you said in your comment...

-4

Look up Meissel's prime-counting function.

One reference is here:

https://en.wikipedia.org/wiki/Prime-counting_function

marty cohen
  • 110,450