Created
April 20, 2018 20:09
-
-
Save thushw/fdd2248c5f1c03d579d51e2c923f8241 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 first_clear(arr, idx): | |
for i in range(idx, len(arr)): | |
if arr[i] == 0: | |
return i | |
return -1 | |
def find_all_primes_upto(n): | |
sieve = [0] * (n+1) | |
i = 2 | |
while i>-1: | |
idx = i | |
while i+idx < n+1: | |
i += idx | |
sieve[i] = 1 | |
i = first_clear(sieve, idx+1) | |
return [i for i in range(2, len(sieve)) if sieve[i]==0] | |
def find_two_primes_sum_to(n): | |
primes = find_all_primes_upto(n) | |
d = set() | |
for prime in primes: | |
other = n - prime | |
if other in d: | |
return other, prime | |
else: | |
d.add(prime) | |
return None | |
a = [100, 489932, 1000000, 6788, 234, 1290] | |
for e in a: | |
print (find_two_primes_sum_to(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment