Skip to content

Instantly share code, notes, and snippets.

@rawsh
Created February 1, 2017 05:01
Show Gist options
  • Select an option

  • Save rawsh/9e1356b6192c68754b471235de77a253 to your computer and use it in GitHub Desktop.

Select an option

Save rawsh/9e1356b6192c68754b471235de77a253 to your computer and use it in GitHub Desktop.
from collections import Counter
def primes(n):
if n==2: return [2]
elif n<2: return []
s=range(3,n+1,2)
mroot = n ** 0.5
half=(n+1)/2-1
i=0
m=3
while m <= mroot:
if s[i]:
j=(m*m-3)/2
s[j]=0
while j<half:
s[j]=0
j+=m
i=i+1
m=2*i+3
return [2]+[x for x in s if x]
def distance(n):
p = primes(n)
differences = []
for x in range(1,len(p)):
differences.append(p[x]-p[x-1])
print Counter(differences).most_common()
distance(100000) # (6, 1940), (2, 1224), (4, 1215)
# 6 is by far the most common difference between primes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment