Approximation to the prime counting function



Is there a function similar to PrimePi that gives approximate value for large numbers?

In particular, I need a reasonably good approximation for $\pi(x)$, where $x\approx10^{1000}$.

More or less a function that gives $\int_2^x\frac{dt}{\ln t}$ or better.

Edit: I settled for the definite integral approximation but if someone knows of a built-in function that performs better, I'm definitely interested.


Posted 2014-09-18T22:04:17.727


2LogIntegral is this integral. Try this: Show[{Plot[LogIntegral[x], {x, 1, 2000}], ListPlot[Table[PrimePi[n], {n, 1, 2000}]]}] – Daniel Lichtblau – 2014-09-18T22:36:49.107



The best analytic built-in approximation is the Riemann Prime Counting Function; it is implemented in Mathematica as RiemannR. So far we know exact values of $\pi$ prime counting function for n < 10^25, however in Mathematica its counterpart PrimePi[n] can be computed exactly to much lower values i.e. up to 25 10^13 -1, see e.g. What is so special about Prime? for more details.

RiemannR[10^1000] // N

See e.g. Prime-counting function, it claims that the best estimator is $ \operatorname{R}(x) - \frac1{\ln x} + \frac1\pi \arctan \frac\pi{\ln x}$

let's define it as APi:

APi[x_] := RiemannR[x] - 1/Log[x] + 1/Pi ArcTan[ Pi/Log[x]]

it provides the same approximation as RiemannR, at least IntegerPart does, in fact

Grid[ Table[ IntegerPart @ { 5 10^k, RiemannR[5 10^k] - PrimePi[5 10^k], 
                             APi[5 10^k] - PrimePi[5 10^k]}, {k, 3, 13}],
      Frame -> All, Alignment -> Left]

enter image description here

We can realize how good RiemanR is plotting related errors of interesting approximations where we know exact values of the prime counting function (see e.g. "Mathematica in Action" by Stan Wagon):

Plot[{ LogIntegral[x] - PrimePi[x], RiemannR[x] - PrimePi[x], 
        x/(Log[x] - 1) - PrimePi[x]},  {x, 2, 3 10^5}, MaxRecursion -> 3, 
      Frame -> True, PlotStyle -> {{Thick, Red}, {Thick, Darker @ Green}, 
                                  {Thick, Darker @ Cyan}}, 
      PlotLegends -> Placed["Expressions", {Left, Bottom}], 
      ImageSize -> 800, AxesStyle -> Thick]

enter image description here

Plot[{ LogIntegral[x] - PrimePi[x], RiemannR[x] - PrimePi[x], 
       x/(Log[x] - 1) - PrimePi[x]}, {x, 10^6, 10^7}, MaxRecursion -> 3, 
       Frame -> True, PlotStyle -> {{Thick, Red}, {Thick, Darker @ Green}, 
                                    {Thick, Darker @ Cyan}}, 
       PlotLegends -> Placed["Expressions", {Left, Bottom}], 
       Axes -> {True, False}, AxesStyle -> Thick, ImageSize -> 800]

enter image description here


Posted 2014-09-18T22:04:17.727

Reputation: 51 831

1Thank you. I wish I could accept both replies as the answer; however, I'm obliged to accept Karsten's correct reply because it was posted first. However, let me stress that I thoroughly enjoyed reading your reply and, particularly, the content of the links you provided. – None – 2014-09-19T00:35:18.670

@user2943324 You can accept whatever you want however you don't have to choose the first answer, moreover accepting too quickly you discourage others (including me) to post another answers possibly better. As far as I can say I could elaborate my answer improving it, even though so far I've provided more details than the other answer. – Artes – 2014-09-19T00:48:32.803

Agreed. It seems that I am too quick to mark a reply as accepted. You have to forgive me. I don't frequent these forums often enough and when I do, it seems, I inadvertently get too excited. – None – 2014-09-19T01:01:13.057


RiemannR seems to be a better choice than LogIntegral based on this plot:

Plot[{PrimePi[n], LogIntegral[n], RiemannR[n]}, {n, 1, 5000}, PlotStyle -> {Black, Blue, Red}]



Karsten 7.

Posted 2014-09-18T22:04:17.727

Reputation: 26 728

1That gets my vote (as does the other RiemannR response). I'll note it is somewhat slower to compute, although still fast enough for whatever purposes. – Daniel Lichtblau – 2014-09-18T23:43:10.277