Monday, March 14, 2016

Mathematicians Discover Prime Conspiracy: Python check


In the article Mathematicians Discover Prime Conspiracy essentially is described the discovery that the last digit of the next prime number is less likely to equal the last digit of its predecessor.
To check this I decided to download a list of the first million prime numbers from here and check.
In [27]:
from collections import defaultdict

this2previous = defaultdict(int)
previous = None
with open('primes1.txt') as f:
    # Skip text file header
    print(f.readline())
    for line in f:
        line = line.strip()
        if not line:
            continue
        for primelastdigit in (prime[-1] for prime in line.split()):
            if previous is not None:
                this2previous[(primelastdigit, previous)] += 1
            previous = primelastdigit
                 The First 1,000,000 Primes (from primes.utm.edu)

In [28]:
def pretty_print(counts):
    lastdigit = None
    for (this, previous), count in sorted(counts.items()):
        if this in '25' or previous in '25':
            continue
        if lastdigit != this:
            print()
        lastdigit = this
        print('%2s %s after %s frequency: %6i' 
              % (('#' if this == previous else ''), this, previous, count))
In [29]:
pretty_print(this2previous)
 # 1 after 1 frequency:  42853
   1 after 3 frequency:  58255
   1 after 7 frequency:  64230
   1 after 9 frequency:  84596

   3 after 1 frequency:  77475
 # 3 after 3 frequency:  39668
   3 after 7 frequency:  68595
   3 after 9 frequency:  64371

   7 after 1 frequency:  79453
   7 after 3 frequency:  72827
 # 7 after 7 frequency:  39603
   7 after 9 frequency:  58130

   9 after 1 frequency:  50153
   9 after 3 frequency:  79358
   9 after 7 frequency:  77586
 # 9 after 9 frequency:  42843
As you can see, the frequency of the last digit being followed by the same last digit in consecutive prime numbers (for this sample of the first million primes), is markedly lower.

1 comment:

  1. Also see: https://scipython.com/blog/do-consecutive-primes-avoid-sharing-the-same-last-digit/

    And: http://kevingong.com/Math/NextPrime.html

    ReplyDelete