7
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ LogIntegral is this integral. Try this: Show[{Plot[LogIntegral[x], {x, 1, 2000}], ListPlot[Table[PrimePi[n], {n, 1, 2000}]]}] $\endgroup$ – Daniel Lichtblau Sep 18 '14 at 22:36
9
$\begingroup$

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]

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

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – user11356 Sep 19 '14 at 0:35
  • $\begingroup$ @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. $\endgroup$ – Artes Sep 19 '14 at 0:48
  • $\begingroup$ 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. $\endgroup$ – user11356 Sep 19 '14 at 1:01
6
$\begingroup$

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

primePiPlot

RiemannR[1.*10^1000]
4.344832576401197453*10^996
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Daniel Lichtblau Sep 18 '14 at 23:43