Here are some code snippets in various languages that compute the Basel Problem:

To begin, $\LaTeX$

$$\sum_{n=1}^\infty \frac{1}{n^2} = \frac{\pi^2}{6} = 1.6449340668482264$$

## Python#

def pi_squared_over_6(N: int) -> float:
return sum(x**(-2) for x in range(1,N))


## Rust#

fn pi_squared_over_6(N: u64) -> f64 {
(1..N).map(|x| 1.0 / ((x*x) as f64)).sum()
}


piSquaredOver6 :: Integer -> Double
-- no capital N in Haskell :(

## Fixing Python#

The python code took an absurdly long amount of time to run, so lets fix it by taking advantage of numpy, which calls into vectorized C code.

import numpy as np

def pi_squared_over_6(N: int) -> float:
x = np.ones(N)
r = np.arange(1,N)
sq = np.square(r)
div = np.divide(x, sq)
return float(np.sum(div))


A bit better, but when I’m checking btm, the excessive memory consumption suggests most of the work done is moving around billions of floats, not the actual arithmetic. Lets try splitting this into chunks:

def pi_squared_over_6(N: int) -> float:
CHUNKS = 25000
SIZE = N // CHUNKS
s = 0.0
x = np.ones(N // CHUNKS - 1)
for i in range(CHUNKS):
N_tmp = i * SIZE
r = np.arange(N_tmp + 1, N_tmp + SIZE)
sq = np.square(r)
div = np.divide(x, sq)
s += np.sum(div)
# deallocate memory
del sq
del div
del r

return s


Much better! Now it’s running under 2 seconds!