## Approximating Pi

### July 29, 2011

In the two previous exercises we have seen three different methods for computing the prime-counting function π(*n*), all based on a formula of Legendre. We have also seen the difficulty of the calculation, as three eminent mathematicians got their sums wrong, and the difficulty continues even today with the use of computers, as Xavier Gourdon had to stop an attempt to compute π(10^{23}) because of an error. Given the difficulty of calculation, in some cases it might make sense to calculate an approximate value of π, instead of an exact value. In today’s exercise we look at two methods for approximating the prime-counting function.

The first method dates to the German mathematician Carl Friedrich Gauss, who gives an estimate π(*x*) ∼ Li(*x*), the logarithmic integral—this is the celebrated *prime number theorem*, which Gauss figured out when he was fifteen years old! The logarithmic integral can be calculated by expanding the infinite series

to the desired degree of accuracy; somewhere around a hundred terms ought to be sufficient for arguments up to 10^{12}. Beware the singularity at *x*=0.

In 1859, Bernhard Riemann, whose hypothesis about the prime numbers is one of the greatest open questions in mathematics, described a different, and much more accurate, approximation to the prime counting function:

where μ(n) is the Möbius function that takes the value 1 when *n* is 1, 0 when the factorization of *n* contains a duplicated factor, and (−1)^{k}, where *k* is the number of factors in the factorization of *n*, otherwise. There is no better way to compute the Möbius function than to compute the factors of *n*. As with the logarithmic integral, about a hundred terms of the infinite series ought to be sufficient to get a good approximation for arguments up to 10^{12}.

Your task is to write functions to compute the logarithmic integral, the Möbius function, and Riemann’s R function, and to compare the results to the values you calculated for the prime-counting function in the previous exercise. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Here’s my Python solution; it reuses

`primes`

, a variation of the Python Cookbook’s version of the Sieve of Eratosthenes.Clojure version, uses O’Neil sieve to get list of primes. Results for Riemann function matches table above.

You have definied series expansion for li(x).

Li(x) = li(x) – li(2).

li(x) ~ pi(x)

I’ll answer this question in a different manner than it was posed in, since the Riemann R-function can be rewritten in a much more numerically-tractable form as the Gram series.

This then requires implementing the Riemann zeta function. To compute this, I use the Dirichlet eta function to convert it to an alternating series and then use an elementary convergence-acceleration technique on the alternating series: