Created
April 20, 2012 15:51
-
-
Save shieldsd/2429852 to your computer and use it in GitHub Desktop.
Project Euler #50
This file contains hidden or 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
from math import sqrt | |
from itertools import takewhile, dropwhile, count | |
def prime(): | |
primes = [] | |
for n in count(2): | |
composite = 0 | |
for p in primes: | |
if not n % p: | |
composite = 1 | |
break | |
elif p ** 2 > n: | |
break | |
if not composite: | |
primes.append(n) | |
yield n | |
def isprime(n): | |
for x in range(2, int(sqrt(n)) + 1): | |
if n % x == 0: | |
return 0 | |
return 1 | |
def csum(it): | |
s = 0 | |
for n in it: | |
s += n | |
yield s | |
print max(j - i | |
for i in takewhile(lambda n: n < 1000000, csum(prime())) | |
for j in dropwhile(lambda n: n < i, | |
takewhile(lambda n: n < 1000000, csum(prime()))) | |
if j - i < 1000000 and isprime(j - i)) | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment