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
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
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:
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:
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)\):
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
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
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
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.