Gaps between primes
One of my favourite YouTube channels is Matt Parker's Stand-up Maths. The videos are full of fun mathematical oddities and I always come away wanting to play with those oddities myself.
In one recent video he discussed the gaps between consecutive prime numbers, with those gaps getting on average longer as the prime numbers get bigger. In the screenshot below, Matt is showing a histogram of the gaps between all consecutive primes less than N=150 million. The most common gap size in this histogram is 6.
My mind was then blown when Matt revealed that 6 is only the most common gap until N=1.74x10^35, when it is overtaken by 30. 30 itself is overtaken by 210 at N=10^425, and so on. These are the primorial numbers: 6=2x3, 30=2x3x5, 210=2x3x5x7. There is an open conjecture that every primorial number eventually becomes the most common gap size as N tends to infinity!
I wanted to visualise that transition for myself. Rather than computing the gaps between all the consecutive primes less than N, I instead calculated the gaps between the first 1,000 primes after 10^n for n in (5,6,7,..,100). The interactive plot below shows the distribution of those gaps for a given n, with n being controlled by the slider. The points are coloured by how many unique prime factors that gap-size has.
The slope of the distribution gets flatter as we increase n, but individual gap-sizes receive a boost to their frequency that depends on how primorial-y that gap-size is. As N increases the distribution eventually becomes so flat that the 'boost' from a number being primorial is enough to overcome the advantage that earlier primorial numbers had from being further up the slope.
I calculated these distributions using code that already existed:
- I took an amazing implementation of a next_prime() function from StackExchange.
- I modified some code I found in a Tweet to histogram the first 1,000 prime gaps after a given number.
- I created the interactive plot using the Bokeh library.

Comments
Post a Comment