Created
December 31, 2016 18:42
-
-
Save paulhankin/c471b24c9a4717010681f0c5e33d6be9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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