Programming Praxis

A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer

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 π(1023) 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

$\mathrm{Li}(x) = \int_0^x \frac{d\ t}{\log_e t} = \gamma + \log \log x + \sum_{k=1}^\infty \frac{(\log x)^k}{k!\ k}$

to the desired degree of accuracy; somewhere around a hundred terms ought to be sufficient for arguments up to 1012. 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:

$\mathrm{R}(x) = \sum_{n=1}^\infty \frac{\mu(n)}{n} \mathrm{Li}(x^{1/n})$

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 1012.

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.

Like this:

Pages: 1 2

Posted by programmingpraxis
Filed in Exercises
2 Comments »

2 Responses to “Approximating Pi”

1. Graham said
August 1, 2011 at 2:51 PM

Here’s my Python solution; it reuses `primes`, a variation of the Python Cookbook’s version of the Sieve of Eratosthenes.

```from math import log
from operator import mul

GAMMA = 0.57721566490153286061

def li(x):
    return GAMMA + log(log(x)) + sum(pow(log(x), n) /
                                     (reduce(mul, xrange(1, n + 1)) * n) for
                                      n in xrange(1, 101))

def factors(n):
    return (p for p in primes(n + 1) if n % p == 0)

def mu(n):
    m = lambda p: -1 if (n / p) % p else 0
    return reduce(mul, (m(p) for p in factors(n)), 1)

def r(x):
    return sum(mu(n) / n * li(pow(x, 1.0 / n)) for n in xrange(1, 101))
```
2. David said
August 25, 2012 at 3:36 AM

Clojure version, uses O’Neil sieve to get list of primes. Results for Riemann function matches table above.

```(load "lazy-sieve")

(defn factors
   "list of factors of n"
   [n]
   (loop [n n, prime lazy-primes, l (vector-of :int)]
      (let [p (first prime)]
         (cond
            (= n 1)  l
            (= (mod n p) 0)
               (recur (/ n p) prime (conj l p))
            :otherwise
               (recur n (rest prime) l)))))

(defn calc-mu
   "Calculate mu(n) for use in building a static array, n > 1"
   [n]
   (let [fac (factors n)]
      (if (not= (count fac) (count (distinct fac)))
         0
         (reduce * (map (fn [x] -1) fac)))))

(def mu (into (vector-of :int 0 1)
   (map calc-mu (range 2 101))))

(defn powers [x]
   (iterate (partial * x) x))

(defn float-factorials []
   (map first (iterate (fn [[n,i]] [(* n i) (inc i)]) [1.0 2])))

(defn logint
   "Compute gauss' logarithmic integral to N terms"
   [n, x]
   (let [gamma 0.5772156649015329,
         ln-x  (Math/log x),
         sum   (reduce +
                  (map #(/ %1 (* %2 %3))
                     (take n (powers ln-x))
                     (take n (float-factorials))
                     (range 1 (inc n))))]
      (+ gamma (Math/log ln-x) sum)))

(defn riemann
   "Compute riemann function, max 100 terms"
   [x]
   (reduce +
      (map #(* (/ (mu %1) %1) (logint 100 (Math/pow x (/ 1 %1))))
         (range 1 101))))
```
%d bloggers like this:
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 2, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.8936589360237122, "perplexity_flag": "middle"}