What entropy measures when it is applied to something with no randomness at all
This text is in dialogue with my series on entropy. The case here is mathematical rather than physical, and it serves to show where an analogy holds and where it starts to promise too much. I write close to the edge of what I know: whatever is my own opinion or inference, I mark as such.
The Uncomfortable Question
In 1972, according to the version that gets told most often, the mathematician Hugh Montgomery was having tea at the Institute for Advanced Study in Princeton when he was introduced to Freeman Dyson. Montgomery had just obtained a formula about the distances between the zeros of the Riemann zeta function. Dyson, who worked in nuclear physics, recognized it immediately: it was the formula that describes how the energy levels of a heavy nucleus are spaced.
That coincidence forces an uncomfortable question. The prime numbers are the most rigid object we know, and not one of their features is decided by chance. Entropy, on the other hand, measures disorder and uncertainty. What sense does it make to measure entropy where everything is determined?
The quick answer is none. But that answer confuses the mechanism with the description. A deterministic system can be so hard to anticipate in detail that, seen from outside, it behaves like a random one subject to certain rules, and entropy measures precisely the distance between what we know of those rules and what we can predict. In this article I go through three places where entropy shows up in the primes and rank them by how well each one holds up. My thesis is that the first two hold up well, and that the third is a metaphor that should be handled with care.
What Entropy Measures
In 1948 Claude Shannon defined the entropy of a probability distribution as the average surprise of its outcomes:
With the natural logarithm the unit is the nat; with base 2 it would be the bit. A loaded coin that almost always lands heads is rarely surprising and has low entropy. A fair coin surprises as much as a coin can.
For the primes the starting point is the prime number theorem: the probability that a large integer is prime is about . The primes thin out, and they do so so regularly that the average gap around is almost exactly . That average is all that pure chance needs to describe the density. What follows consists of comparing the exact location of each prime against that chance.
A Gas Made of Primes
In the eighteenth century Euler discovered that the zeta function can be written in two equivalent ways, a sum over all the integers and a product over the primes:
In 1990 Bernard Julia and, independently, Donald Spector read that equality with a physicist’s eyes. They imagined a quantum gas of free bosons, one for each prime, in which the particle has energy (in units where ). A state with several particles is a product of primes with multiplicities, , and its total energy is the sum of the energies, that is, . The partition function, which in statistical mechanics contains all the thermodynamics of a system, turns out to be:
Here . The equality is exact, and the uniqueness of prime factorization is what allows the particles to be treated as independent.
With in hand, the entropy is obtained as in any other system:
The remarkable thing happens as approaches 1. The harmonic series diverges, grows like and the entropy goes to infinity. The temperature works as a Hagedorn temperature: there are about integers with , so the number of states grows exponentially with energy, and adding energy creates new combinations of primes instead of heating the gas. Above that temperature the partition function ceases to exist. In other words, the divergence of that is taught in a first analysis course is, seen from physics, the stability limit of an arithmetic gas.
It is worth being precise about the status of this object. It is an exact mathematical construction, but its particles do not interact and it does not exist in any laboratory. There is also a more sophisticated cousin, the Bost–Connes system (1995), which has the same partition function and does present a phase transition with spontaneous symmetry breaking at . As far as I know, neither of the two has produced a new result about the distribution of the primes. Their value has been to give a physical vocabulary to what was already known and to suggest questions.
Zeros That Repel
The second appearance of entropy is more indirect and deeper. The primes are encoded in the non-trivial zeros of the zeta function: the explicit formula of Riemann and von Mangoldt expresses the fluctuations in the count of primes as a sum of waves, one for each zero. The Riemann hypothesis states that all those zeros have the form with real.
What Montgomery computed was the statistics of the distances between consecutive zeros. If the zeros were points thrown at random onto a line, a Poisson process, they would often bunch together in clusters. They do not. The probability of finding two zeros very close together tends to zero, a phenomenon physicists call level repulsion. The formula coincides with that of the Gaussian unitary ensemble of random matrices, the one that describes the energy levels of heavy nuclei. In the 1980s Andrew Odlyzko checked with massive computations that the agreement holds with remarkable precision in regions very far along the line.
The Hilbert–Pólya conjecture, never proven, says that there exists a self-adjoint operator whose spectrum is those zeros. In 1999 Berry and Keating proposed that this operator would be the quantization of a chaotic classical system. It is the most serious lead there is, and also a good example of what I want to distinguish: the zeros behave like the levels of a chaotic system, and nobody has proven that they are. Moreover, this result is about the zeros and not about the gaps between primes, which is the subject of what follows.
Measuring the Disorder of the Gaps
If we want to measure the disorder of the primes themselves, the natural object is the gaps . The reference model was proposed by Harald Cramér in 1936: each integer is prime with probability , independently of the others. Under that model, once normalized by the local mean gap, the gaps follow an exponential distribution. The exponential with mean 1 is the maximum-entropy distribution among positive distributions with that mean, and its differential entropy is exactly 1 nat. That is the ceiling: the disorder of a gas of primes with no arithmetic rule at all.
There is a complication. Real gaps only take even values, because every prime greater than 2 is odd. To compare cleanly it is better to work with the gaps as integers, and to use as the ceiling the maximum-entropy distribution over the even numbers with the same mean , which is a geometric distribution. With , its entropy is:
Since the means coincide and the logarithm of a geometric distribution is linear in , the difference from the empirical entropy is exactly the Kullback–Leibler divergence:
It is the amount of deterministic information that survives in the sequence of gaps, measured against chance with no rules.
I did the computation with a segmented sieve in Python (the code is available for anyone who wants to repeat it), over all the primes up to (5,761,454 gaps) and over two windows of between seven and nine million primes near and . The deficit came out at 0.059 nats for the primes below , 0.057 for those below , 0.057 near and 0.058 near . That is a little over 1.5% of the total entropy, about 0.08 bits.
What stands out is that it barely changes. The natural expectation was that the deficit would shrink toward zero as we climb, as if arithmetic were forgetting its rules. It does not.

The black curve is what a gas of primes would give with no arithmetic rule at all. The bars are the real gaps near , and the difference between the two is the deficit. You can see at a glance where it is concentrated: the blue bars, the gaps that are multiples of 6, stand above the curve, with peaks at 6, 12 and 18, and the gray ones fall below it. The gaps of 2 and 4, the most famous ones, sit well below chance. At the mean gap is almost 28, and even so the sequence still prefers multiples of 6.
At a coarse scale there is convergence. The differential entropy of the normalized gaps, estimated with intervals of 0.25, rises from 0.85 for the primes below to 0.98 near , approaching the ceiling of 1. (With wider intervals the estimator exceeds 1 in the last window, which is impossible for the true value, so the final decimals of this estimate are a bias of the method and not a measurement.) The two results do not contradict each other. At a coarse scale the primes approach chance, and at a fine scale they keep a trace that does not fade.
The Trace That Does Not Fade
The main cause lies in the smallest primes. Past 3, a prime has the form or . If it is , a gap of 2 would take it to , a multiple of 3 that cannot be prime. That restriction favors the gaps that are multiples of 3, and something similar, weaker, happens with 5, 7 and the primes that follow. Hardy and Littlewood quantified the effect in 1923 with what is called the singular series, and their conjecture predicts that, in the limit, a gap of 6 is twice as frequent as a gap of 2. The measured frequencies approach that ratio very slowly: 1.66 up to , 1.75 up to , 1.81 near and 1.84 near . That is why 6 is the most common gap over an enormous stretch of the line, up to the order of , where according to the work of Odlyzko, Rubinstein and Wolf on jumping champions it is replaced by 30.
There is a computation that I find illuminating, and I present it as an inference of mine. If one averages the term over all even gaps, with the normalized singular series, the result is a deficit of the order of 0.07 nats, the same magnitude as the one measured. It is not a result, because it ignores that the gaps are between consecutive primes and not between any pair of primes. It suggests, however, that the trace is structural and not an accident of the ranges I was able to compute.
In 1976 Gallagher proved that the Hardy–Littlewood prime k-tuples conjecture implies that, at the scale of the mean gap, the primes are distributed like a Poisson process. It is worth noticing the conditional: the conjecture is not proven and, therefore, neither is the Poisson character of the gaps. What the result lets us see is a coexistence, Poisson at a coarse scale and arithmetic at a fine scale.
Neighboring gaps are not independent either. The correlation between consecutive gaps is negative and small: for the primes below and near . The mutual information between a gap and the next one is around 0.2 nats (0.245 in the first range and 0.212 in the last) and it falls very slowly. I checked that it is not an artifact of the sample size: with the Miller–Madow correction the value hardly moves, and if the order of the gaps is shuffled the mutual information drops below 0.001. Almost all of it is explained by an elementary rule. A prime greater than 3 leaves remainder 1 or 2 when divided by 3, so two consecutive gaps cannot both leave remainder 1, nor both leave remainder 2, because the second would lead to a multiple of 3. Looking only at the remainder of each gap modulo 3 already recovers about 0.20 of the 0.21 nats. It is the same mechanism as the bias that Lemke Oliver and Soundararajan discovered in 2016: a prime ending in 1 is less likely to be followed by another prime ending in 1 than chance would suggest. They measure last digits and not gaps, but both point to the same phenomenon.
Three Questions to Order the Evidence
So far there are claims of different weight, and to order them I use the three-question test that I develop at the end of my series on entropy and that works for any analogy between disciplines: whether there is a formal homology, whether there is a bounded physical identity, and whether there is an extrapolation that nobody guarantees.
The gas of primes passes the first with top marks, because is an equality, and passes the second halfway, because it is a well-defined system but without interactions and without a material realization. The zeros and the random matrices pass the first with overwhelming numerical evidence and without proof. The entropy deficit of the gaps is a measurement and not an analogy, and its interpretation, arithmetic constraints that do not dissipate, rests on the heuristic theory of Hardy and Littlewood.
Different is the step that is sometimes added. It is said that a prime is pure information, the atom that resists all compression, and that the density is the minimum rate of entropy that arithmetic needs in order not to fall into finite cycles. My opinion is that this works as an image and fails as a claim. A composite number is not more compressible for having factors: describing with and large takes almost as many bits as describing a prime of the same size. And the -th prime is obtained from its index , so its Kolmogorov complexity does not exceed that of the index plus a constant. For algorithmic information theory, a prime is no more incompressible than an arbitrary integer of its size. As far as I know, there is no informational derivation of why arithmetic would need that rate of primes.
What Remains Open
Two things remain unresolved, and I think they are related. The repulsion of the zeros and the persistent deficit of the gaps may be two ways of saying the same thing, or may have nothing to do with each other. If the Hilbert–Pólya operator exists, what one would want is for its spectrum to explain, through the explicit formula, why the gaps remember the small primes and become Poissonian at a coarse scale. That is a wish and not a proven consequence.
Meanwhile there is the figure: about 0.057 nats, which does not shrink between a million and a trillion. It is what it costs to think of the primes as chance, measured in the unit with which Shannon measures chance. And what if the deficit never goes away? Then arithmetic would be, at every finite scale, a little less free than it looks, and our intuition that the primes behave like dice loaded by a simple rule would have been right to a greater extent than we can prove today.
Sources
- Shannon, C. E. (1948). “A Mathematical Theory of Communication”. Bell System Technical Journal, 27.
- Julia, B. L. (1990). “Statistical theory of numbers”. In Number Theory and Physics, Springer Proceedings in Physics 47.
- Spector, D. (1990). “Supersymmetry and the Möbius inversion function”. Communications in Mathematical Physics, 127, 239–252.
- Bost, J.-B. and Connes, A. (1995). “Hecke algebras, type III factors and phase transitions with spontaneous symmetry breaking in number theory”. Selecta Mathematica, 1, 411–457.
- Montgomery, H. L. (1973). “The pair correlation of zeros of the zeta function”. Proceedings of Symposia in Pure Mathematics, 24, 181–193.
- Odlyzko, A. M. (1987). “On the distribution of spacings between zeros of the zeta function”. Mathematics of Computation, 48, 273–308.
- Berry, M. V. and Keating, J. P. (1999). “The Riemann zeros and eigenvalue asymptotics”. SIAM Review, 41, 236–266.
- Cramér, H. (1936). “On the order of magnitude of the difference between consecutive prime numbers”. Acta Arithmetica, 2, 23–46.
- Hardy, G. H. and Littlewood, J. E. (1923). “Some problems of ‘Partitio numerorum’ III: On the expression of a number as a sum of primes”. Acta Mathematica, 44, 1–70.
- Gallagher, P. X. (1976). “On the distribution of primes in short intervals”. Mathematika, 23, 4–9.
- Odlyzko, A., Rubinstein, M. and Wolf, M. (1999). “Jumping champions”. Experimental Mathematics, 8, 107–118.
- Lemke Oliver, R. J. and Soundararajan, K. (2016). “Unexpected biases in the distribution of consecutive primes”. Proceedings of the National Academy of Sciences, 113, E4446–E4454.
- Own computations: entropia_huecos.py, segmented sieve in Python with primes up to and windows near and .