The Riemann zeta function is the infinite sum $$\zeta(z) = \sum_{n=1}^{\infty} \frac{1}{n^z}, \quad z = a + ib \in \mathbb{C}.$$
To evaluate it we need to make sense of a complex exponent. Writing \(n^z = e^{z \ln n}\) and applying Euler's formula \(e^{ix} = \cos x + i \sin x\) gives $$\frac{1}{n^{a+ib}} = n^{-a}\bigl(\cos(b \ln n) - i \sin(b \ln n)\bigr),$$ which translates directly into code:
function zeta(z, limit){
var zres = new Complex(0, 0);
for(var x = 1; x <= limit; x++){
var ii = z.imaginary * Math.log(1/x);
var pp = Math.pow(1/x, z.real);
zres.real += pp * Math.cos(ii);
zres.imaginary += pp * Math.sin(ii);
}
return zres;
}
The series converges for \(\operatorname{Re}(z) > 1\). Drag and zoom the plane below to explore the partial sums.
We get a more complete picture by observing how the zeta function transforms the entire complex plane.
Enter an interval and the density of points.
The series above only converges for \(\operatorname{Re}(z) > 1\), but the function it defines turns out to be the restriction of a much larger one. Analytic continuation is the idea that a "nice enough" (holomorphic) function is rigid: once you know it on a small region, there is at most one way to extend it to a larger one. For zeta, a standard rearrangement of the series produces such an extension, $$\zeta(z) = \frac{1}{z-1}\sum_{n=1}^{\infty}\left(\frac{n}{(n+1)^z} - \frac{n-z}{n^z}\right),$$ which now converges for \(\operatorname{Re}(z) > 0\), \(z \neq 1\).
For some values in \(0 < Re(z) < 1\) the zeta function equals to zero. Riemann hypothesized that all those zeros have a real value of \(0.5\). Here the black dots along 0.5 are evaluated by the extended zeta function and rendered in red. Some of them converge to \(0 + 0i\). These are the non trivial zeta zeros.
The function $$\psi(x) = \sum_{n \leq x}^{} \Lambda(n)$$ was introduced by Chebyshev, which counts all powers of primes. Each \(p^k, k \in \mathbb{N}\) is weighted \(\log(p)\). (Seen in black)
This function can also be expressed with a sum of the non trivial zeta zeros \(\rho\). Thats all places the zeta function evaluates to 0,0 when its real input was between 0 and 1. $$\psi_{0}(x) = x - \ln(2\pi) - \sum_{\rho}^{\infty}{\frac{x^\rho}{\rho}} - \frac{1}{2}\ln(1-\frac{1}{x^2})$$
As all non trivial zeros come in conjugate pairs e.g. \(0.5 + 14.13i\) and \(0.5 - 14.13i\), the imaginary part cancels out and we are left with a real function. (Depicted in red)