# How to use the nontrivial zeros to fit the PrimePi?

As this answer, I make a gif in follow:

RiemannJint[x_?NumericQ] :=
NIntegrate[1/(u (u^2 - 1) Log[u]), {u, x, Infinity}]
RiemannJ[x_, k_Integer] := 0 /; x < 2
RiemannJ[x_, k_Integer] :=
LogIntegral[x] - Log[2.] + RiemannJint[x] -
2*Total[Re[ExpIntegralEi[N[ZetaZero[Range[k]]]*Log[x]]]]
SetAttributes[RiemannJ, Listable]

RiemannPi[x_, k_Integer] :=
Sum[(MoebiusMu[n]/n) RiemannJ[x^(1/n), k], {n, 1, k}]

LaunchKernels[];
imgs = Table[
ListLinePlot[{Table[{x, PrimePi[x]}, {x, 2., 50., 0.5}],
ParallelTable[{x, RiemannPi[x, i]}, {x, 2., 50., 0.5}]},
ImageSize -> 350, PlotRange -> {{2, 50}, {0, 16}},
PlotLabel -> (ToString[i] <> " zeros are used")], {i, 5, 500, 5}];

ListAnimate[imgs]


But it's obviously different from the animation provided by wiki here, which uses 200 nontrivial zeros, which perfectly matches the PrimePi function:

I know the formula is $$f(x)=\operatorname{li}(x)-\sum_\rho\operatorname{li}(x^\rho)-\ln 2+\int_x^\infty \frac{\mathrm dt}{t(t^2-1)\ln t}$$, but I don't know how to deal with that $$\sum_\rho\operatorname{li}(x^\rho)$$ with MMA. This is my current try as this post:

countPrime[x_, k_] :=
ExpIntegralEi[(1 - ZetaZero[Range[k]])*Log[x]]]]] - Log[2] +
NIntegrate[1/(t (t^2 - 1)), {t, x, Infinity}]


But it looks a lot different than built-in PrimePi. Can anyone plot the wiki animation using MMA?

• Your sampling interval 0.5 is quite big - so it is not a surprise it does not look nice - especially the slopes at each step are not perpendicular because of this. Commented Oct 20, 2022 at 9:33

If si is sampling interval then it is also a good choice to shift sampling by si/2 in case of function with discontinuities so that the samples fall evenly spaced before and after discontinuity.

So use the following in your code (for even more precise plot use smaller si than 0.5):

si=0.5;
ListLinePlot[{Table[{x, PrimePi[x]}, {x, 2.0 - si/2, 50., si}],
ParallelTable[{x, RiemannPi[x, i]}, {x, 2.0 - si/2, 50., si}]},
ImageSize -> 350, PlotRange -> {{2, 50}, {0, 16}},
PlotLabel -> (ToString[i] <> " zeros are used")]

• Shift sampling doesn't seem to fit the logic of the formula used
– yode
Commented Oct 20, 2022 at 11:06
• Do not understand what you mean. It is not shifting of a function. It is shifting of the choice of samples - it does not affect the function itself - it only affects it appearance of the plot a for big si - for small or negligible si it has no effect at all. Commented Oct 20, 2022 at 12:43