Skip to content

Instantly share code, notes, and snippets.

@shieldsd
Created March 30, 2012 14:14
Show Gist options
  • Save shieldsd/2251796 to your computer and use it in GitHub Desktop.
Save shieldsd/2251796 to your computer and use it in GitHub Desktop.
Project Euler #27
from itertools import takewhile
from itertools import count
from math import sqrt
def isprime(n):
for x in range(2, int(sqrt(n)) + 1):
if n % x == 0:
return 0
return 1
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
print max((sum(1 for _ in
takewhile(lambda x: isprime(x),
(abs(n*n + a*n + b) for n in count()))), a * b)
for a in range(-999, 999, 2)
for b in takewhile(lambda x: x < 1000, prime()))[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment