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.

• LogIntegral is this integral. Try this: Show[{Plot[LogIntegral[x], {x, 1, 2000}], ListPlot[Table[PrimePi[n], {n, 1, 2000}]]}] Sep 18, 2014 at 22:36

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

4.344832576401197453*10^996


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]


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]


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]


• Thank 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.
– user11356
Sep 19, 2014 at 0:35
• @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. Sep 19, 2014 at 0:48
• 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.
– user11356
Sep 19, 2014 at 1:01

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}]


RiemannR[1.*10^1000]

4.344832576401197453*10^996

• That 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. Sep 18, 2014 at 23:43