Skip to content

Instantly share code, notes, and snippets.

@paulhankin
Created December 31, 2016 18:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulhankin/c471b24c9a4717010681f0c5e33d6be9 to your computer and use it in GitHub Desktop.
Save paulhankin/c471b24c9a4717010681f0c5e33d6be9 to your computer and use it in GitHub Desktop.
def primes(n):
x = [False] * (n + 1)
for i in xrange(2, n + 1):
if x[i]: continue
yield i
for j in xrange(2 * i, n + 1, i):
x[j] = True
def prime_pairs(n):
ps = list(primes(n))
i, j = 0, len(ps) - 1
while i <= j:
while i <= j and ps[i] + ps[j] > n:
j -= 1
if i <= j and ps[i] + ps[j] == n:
yield ps[i], ps[j]
i += 1
print list(prime_pairs(10))
print list(prime_pairs(100))
print list(prime_pairs(100000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment