Subsequent Prime Number Distribution

In 2016, two mathematicians, Kannan Soundararajan and Robert Lemke Oliver found that the prime numbers do not occur at random when looking at the final digits of subsequent primes.  For instance, a prime ending in 9 is more likely to be followed by a prime ending in 1, than any other digits (1, 3, 7 or 9).  Also, 9 -> 1 is almost twice as likely to occur than 7 -> 7.  Crazy!

Here is an excellent article about it.  (And here is the Terence Tao post explaining it for the number theory intrepid.)

Being a skeptical programmer, naturally I had to write a simple program to verify these results for myself.  So here goes:

First is the opening of the program.  It declares that it is perl code, that it will be strict about syntax and will issue compile warnings when encountered.  Also, we load up the module that will generate prime numbers for us.

Next, the program declares a few variables to keep track of things:

This loop builds the index of “previous-subsequent” primes and the number of times each pair is seen:

Now we use this index to calculate the percentage of times each pair is seen and then print out the result:

And here are those results for the primes below 10,000,000,000, that are two digits or more (455,052,507 of them):

That is, a prime ending with 9 is more likely to be followed by a prime ending in 1 than all other combinations.  Fascinating!

If each occurred equally, the pairs would all be 1/16 = 0.0625 or 6.25%.