Riemann’s Explicit Formulas

John Baez

October 31, 2026

Here are two staircases built from prime numbers, and two formulas that rebuild each staircase out of waves, one for each zero of the Riemann zeta function. The first staircase counts the primes below any number; the second shows a subtler function that actually works more simply. Press play and watch the waves add up.

Counting primes: \(\pi(x)\)

The staircase counts the primes up to \(x\). The red curve is Riemann’s smooth approximation, corrected by one pair of zeta zeros after another. With each pair the curve bends a little closer to the steps.

N = 0 smooth part only \(\pi(x)\)approximation
Start at \(x =\)
Width

What you are seeing

In 1859 Riemann found an exact formula for the number of primes \(\pi(x)\) less than or equal to \(x\). It expresses \(\pi(x)\) as a smooth function plus a sum of waves, one for each pair of zeros of the zeta function. The nontrivial zeros used here have the form \(\rho_k = \tfrac12 \pm i\gamma_k\), numbered so that \(0 < \gamma_1 < \gamma_2 < \cdots\), from \(\gamma_1 \approx 14.1347\) up to \(\gamma_{1000} \approx 1419.4225\). For every \(x > 1\) that is not a prime, Riemann’s formula says

$$\pi(x) \;=\; S(x) \;+\; \sum_{k=1}^{\infty} W_k(x),$$

where \(S(x)\) is a smooth function and \(W_k(x)\) is a wave coming from the \(k\)th pair of zeros. The animation shows the partial sums

$$\pi_N(x) \;=\; S(x) \;+\; \sum_{k=1}^{N} W_k(x).$$

Frame 0 shows the smooth part \(S(x)\) alone, and each frame \(N\) adds one more wave, \(W_N(x)\).

Each wave is very close to a simple oscillation:

$$W_k(x) \;\approx\; -2\,\mathrm{Re}\!\left[\frac{x^{\rho_k}}{\rho_k \ln x}\Bigl(1 + \frac{1}{\rho_k \ln x}\Bigr)\right] \;\approx\; -\frac{2\sqrt{x}}{|\rho_k|\,\ln x}\,\cos\bigl(\gamma_k \ln x - \arg \rho_k\bigr),$$

where now \(\rho_k = \tfrac12 + i\gamma_k\). So the \(k\)th wave oscillates with frequency \(\gamma_k\) as a function of \(\ln x\), and its amplitude grows like \(\sqrt{x}/\ln x\).

The smooth part \(S(x)\) is close to the logarithmic integral \(\mathrm{li}(x)\), corrected by a term that accounts for squares of primes:

$$S(x) \;\approx\; \mathrm{li}(x) \;-\; \frac{\sqrt{x}}{\ln x}\Bigl(1 + \frac{2}{\ln x} + \frac{8}{\ln^2 x}\Bigr).$$

But these two approximations are not exact! If we put them into Riemann’s formula, the result is only approximately true. To get an exact formula we need three correction terms, which we call \(A(x)\), \(B(x)\) and \(C(x)\):

$$\begin{aligned}\pi(x) \;=\;& \mathrm{li}(x) \;-\; \frac{\sqrt{x}}{\ln x}\Bigl(1 + \frac{2}{\ln x} + \frac{8}{\ln^2 x}\Bigr) \\[4pt] &-\; 2\sum_{\gamma>0} \mathrm{Re}\!\left[\frac{x^{\rho}}{\rho\,\ln x}\Bigl(1 + \frac{1}{\rho \ln x}\Bigr)\right] \\[4pt] &+\; A(x) + B(x) + C(x).\end{aligned}$$

Here \(A(x)\) is the error in our approximation of the smooth part \(S(x)\), while \(B(x)\) and \(C(x)\) together are the errors in our approximations of the waves \(W_k(x)\), summed over all \(k\). These corrections are fairly small, and the animation includes them all: it uses the exact smooth part and the exact waves. To write the corrections down, let \(M = \lfloor \log_2 x \rfloor\), and let \(\mu(n)\) be \(0\) if \(n\) is divisible by a square greater than 1, and otherwise \((-1)^m\) where \(m\) is the number of prime factors of \(n\). Then the smooth part and the waves are

$$S(x) \;=\; \sum_{n=1}^{M} \frac{\mu(n)}{n}\Bigl(\mathrm{li}\bigl(x^{1/n}\bigr) - \ln 2 + \int_{x^{1/n}}^{\infty}\frac{dt}{t\,(t^2-1)\ln t}\Bigr),$$
$$W_k(x) \;=\; -2\sum_{n=1}^{M} \frac{\mu(n)}{n}\,\mathrm{Re}\,\mathrm{li}\bigl(x^{\rho_k/n}\bigr).$$

The \(n = 1\) term of \(W_k(x)\) is \(-2\,\mathrm{Re}\,\mathrm{li}(x^{\rho_k})\), and its two-term expansion is the main wave above. In \(S(x)\), the \(n = 1\) term gives \(\mathrm{li}(x)\) and the \(n = 2\) term gives \(-\tfrac12\mathrm{li}(\sqrt{x})\), whose three-term expansion is the \(\sqrt{x}\) term. Everything left over is

$$\begin{aligned}A(x) \;=\;& -\Bigl[\tfrac12\,\mathrm{li}(\sqrt{x}) - \frac{\sqrt{x}}{\ln x}\Bigl(1 + \frac{2}{\ln x} + \frac{8}{\ln^2 x}\Bigr)\Bigr] \\[4pt] &+\; \sum_{n=3}^{M} \frac{\mu(n)}{n}\,\mathrm{li}\bigl(x^{1/n}\bigr) \\[4pt] &+\; \sum_{n=1}^{M} \frac{\mu(n)}{n}\Bigl(\int_{x^{1/n}}^{\infty}\frac{dt}{t\,(t^2-1)\ln t} \;-\; \ln 2\Bigr),\end{aligned}$$
$$B(x) \;=\; -2\sum_{\gamma>0} \mathrm{Re}\!\left[\mathrm{li}(x^{\rho}) - \frac{x^{\rho}}{\rho\,\ln x}\Bigl(1 + \frac{1}{\rho \ln x}\Bigr)\right],$$
$$C(x) \;=\; -2\sum_{\gamma>0}\, \sum_{n=2}^{M} \frac{\mu(n)}{n}\,\mathrm{Re}\,\mathrm{li}\bigl(x^{\rho/n}\bigr).$$

The term \(B(x)\) is tiny: about \(2\times 10^{-6}\) at \(x = 1000\) with 1000 zeros. The terms in \(C(x)\) have size about \(x^{1/4}\) rather than \(x^{1/2}\); together with the smooth terms in \(A(x)\), they cancel the steps at squares, cubes and higher powers of primes that the main waves would otherwise produce. At \(x = 1000\) we have \(A \approx -2.60\) and, with 1000 zeros, \(C \approx -0.12\).

This is a rearrangement of Riemann’s own formula. He counted prime powers with weights, \(J(x) = \sum_{p^k \le x} 1/k\), proved that

$$J(x) \;=\; \mathrm{li}(x) \;-\; \sum_{\rho} \mathrm{li}(x^{\rho}) \;-\; \ln 2 \;+\; \int_{x}^{\infty}\frac{dt}{t\,(t^2-1)\ln t},$$

and recovered the primes using \(\pi(x) = \sum_{n} \frac{\mu(n)}{n} J(x^{1/n})\). Only the terms with \(n \le M\) appear, since \(J(y) = 0\) for \(y < 2\).

The wave \(W_k(x)\) is roughly a cosine of \(\gamma_k \ln x\), so it goes through one full cycle each time \(\ln x\) increases by \(2\pi/\gamma_k\). Near any given \(x\), that means one cycle each time \(x\) increases by about \(\Delta x \approx 2\pi x/\gamma_k\). For the fastest wave in the animation, the one coming from \(\gamma_{1000}\), this is about \(0.44\) near \(x = 100\), about \(2.2\) near \(x = 500\) and about \(4.4\) near \(x = 1000\). So 1000 pairs resolve every step below 100 and most steps near 500, while near 1000 they separate primes that are more than a few units apart but blur close pairs together.

For \(x < 2\) every \(\pi_N(x)\) is exactly 0. The smooth part has small jumps at \(x = 2, 4, 8, 32, 64, 128\) and \(1024\), where a new term \(J(x^{1/n})\) switches on; the zero terms smooth these out as \(N\) grows. At a prime the full formula gives the midpoint of the jump, and the truncated sums show the usual overshoot on either side of each step.

All of this is rather complicated. It turns out we can capture the essence of what’s going on in Riemann’s formula for \(\pi(x)\) in a simpler way, using a different function called \(\psi(x)\).

Counting prime powers with weights: \(\psi(x)\)

Now, instead of a staircase that counts primes up to \(x\), let’s look at a staircase that adds up \(\ln p\) for every prime power \(p^k\) up to \(x\). The red curve here starts as a straight line and is corrected by one pair of zeta zeros after another. With each pair it bends a little closer to the steps.

N = 0 smooth part only \(\psi(x)\)approximation
Start at \(x =\)
Width

What you are seeing

The von Mangoldt function \(\Lambda(n)\) equals \(\ln p\) if \(n\) is a power of a prime \(p\), and \(0\) otherwise. Its running total

$$\psi(x) \;=\; \sum_{n \le x} \Lambda(n) \;=\; \sum_{p^k \le x} \ln p$$

is a staircase that jumps by \(\ln 2\) at \(2, 4, 8, 16, \dots\), by \(\ln 3\) at \(3, 9, 27, \dots\), and so on. The prime number theorem says that \(\psi(x)\) is asymptotic to \(x\).

In 1895 von Mangoldt proved the following exact formula, in the spirit of Riemann’s formula for the number of primes. For \(x > 1\) not a prime power,

$$\psi(x) \;=\; x \;-\; \sum_{\rho} \frac{x^{\rho}}{\rho} \;-\; \ln 2\pi \;-\; \tfrac12 \ln\bigl(1 - x^{-2}\bigr).$$

The sum runs over the nontrivial zeros \(\rho\) of the zeta function, taken in order of increasing \(|\mathrm{Im}\,\rho|\). Each term has a simple origin: \(x\) comes from the pole of the zeta function at \(s = 1\), the sum from its nontrivial zeros, \(\ln 2\pi\) from \(s = 0\), and the last term from the zeros at \(s = -2, -4, -6, \dots\), since \(\sum_{n \ge 1} x^{-2n}/2n = -\tfrac12 \ln(1 - x^{-2})\).

The zeros used here all have the form \(\rho = \tfrac12 + i\gamma\), and they come in conjugate pairs. Each pair contributes a single wave:

$$\frac{x^{\rho}}{\rho} + \frac{x^{\bar\rho}}{\bar\rho} \;=\; \frac{2\sqrt{x}}{|\rho|}\,\cos\bigl(\gamma \ln x - \arg \rho\bigr).$$

This wave oscillates with frequency \(\gamma\) as a function of \(\ln x\), and its amplitude \(2\sqrt{x}/|\rho|\) grows like \(\sqrt{x}\). The red curve after \(N\) steps is

$$\psi_N(x) \;=\; x \;-\; \ln 2\pi \;-\; \tfrac12 \ln\bigl(1 - x^{-2}\bigr) \;-\; \sum_{k=1}^{N} \frac{2\sqrt{x}}{|\rho_k|}\,\cos\bigl(\gamma_k \ln x - \arg \rho_k\bigr).$$

Step 0 is the smooth part alone, which is almost the straight line \(y = x\). Each later step adds the wave from the next pair of zeros \(\tfrac12 \pm i\gamma_k\), from \(\gamma_1 \approx 14.1347\) to \(\gamma_{1000} \approx 1419.4225\). The \(k\)th wave is a cosine of \(\gamma_k \ln x\), so it goes through one full cycle each time \(\ln x\) increases by \(2\pi/\gamma_k\). Near any given \(x\), that means one cycle each time \(x\) increases by about \(\Delta x \approx 2\pi x/\gamma_k\). For the fastest wave in the animation, the one coming from \(\gamma_{1000}\), this is about \(0.44\) near \(x = 100\), about \(2.2\) near \(x = 500\) and about \(4.4\) near \(x = 1000\), so close prime powers near 1000 stay blurred together.

At a prime power the full formula gives the midpoint of the jump, and the truncated sums show the usual overshoot on either side of each step. Compared with the formula for the number of primes, everything here is elementary: the waves are plain cosines times \(\sqrt{x}\), with no logarithmic integrals and no sum over roots \(x^{1/n}\). Weighting each prime power by \(\ln p\) is exactly what makes this happen.

The weight \(\ln p\) is also natural because it sets up a connection between the von Mangoldt function and the Riemann zeta function. Taking the logarithm of Euler’s product \(\zeta(s) = \prod_p (1 - p^{-s})^{-1}\) and differentiating gives, for \(\mathrm{Re}\, s > 1\),

$$-\frac{\zeta'(s)}{\zeta(s)} \;=\; \sum_{n=1}^{\infty} \frac{\Lambda(n)}{n^{s}} .$$

Furthermore, we have

$$-\frac{\zeta'(s)}{\zeta(s)} \;=\; s\int_1^\infty \psi(x)\, x^{-s-1}\, dx,$$

where the right-hand side is \(s\) times the Mellin transform of \(\psi\), evaluated at \(-s\).

References

  1. Bernhard Riemann, “Ueber die Anzahl der Primzahlen unter einer gegebenen Grösse,” Monatsberichte der Berliner Akademie, November 1859. English translation by David R. Wilkins, “On the Number of Prime Numbers less than a Given Quantity” (1998).
  2. Hans von Mangoldt, “Zu Riemanns Abhandlung ‘Ueber die Anzahl der Primzahlen unter einer gegebenen Grösse’,” Journal für die reine und angewandte Mathematik 114 (1895): 255–305, EuDML.
  3. Hans Riesel and Gunnar Göhl, “Some Calculations Related to Riemann’s Prime Number Formula,” Mathematics of Computation 24 (1970): 969–983, doi:10.1090/S0025-5718-1970-0277489-3. The first plots of this kind, using 29 pairs of zeros.
  4. Andrew Odlyzko, Tables of zeros of the Riemann zeta function. The numbers \(\gamma_k\) used here were computed with mpmath and agree with these tables.