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.

1 2 3 4 |
#!/usr/bin/env perl use strict; use warnings; use Math::Prime::XS qw(primes); |

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

1 2 3 4 5 6 |
my $max = shift || 100; my @primes = primes( 11, $max ); # All greater-than-1 digit primes below max my %digits; # Bucket for our subsequent primes my $previous; # The previous prime number seen in the loop below |

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

1 2 3 4 5 6 7 8 |
# Collect the "previous - following" digits for my $p ( @primes ) { my $current = substr $p, -1; # Get the last digit if ($previous) { $digits{ $previous . $current }++; # Sum the number of times this pair is seen } $previous = $current; } |

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

1 2 3 4 5 6 |
# Print out the sorted results for my $key ( sort { $digits{$a} <=> $digits{$b} } keys %digits ) { # Set the value to the ratio of the number of times seen per number of primes. $digits{$key} = $digits{$key} / @primes * 100; printf "%s: %.3f %%\n", $key, $digits{$key}; } |

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
77: 4.556 % 33: 4.559 % 11: 4.708 % 99: 4.708 % 19: 5.530 % 31: 6.047 % 97: 6.048 % 93: 6.363 % 71: 6.364 % 73: 6.722 % 37: 6.992 % 13: 7.357 % 79: 7.358 % 39: 7.403 % 17: 7.404 % 91: 7.881 % |

Fascinating!

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