Skip to content

Instantly share code, notes, and snippets.

@shieldsd
Created April 20, 2012 15:50
Show Gist options
  • Save shieldsd/2429847 to your computer and use it in GitHub Desktop.
Save shieldsd/2429847 to your computer and use it in GitHub Desktop.
Project Euler #49
from math import sqrt
from itertools import takewhile, dropwhile, count
def comb(l):
if len(l) == 0:
yield []
else:
for i in range(len(l)):
for cc in comb(l[:i] + l[i + 1:]):
yield [l[i]] + cc
def combn(n):
return [int(''.join(x)) for x in comb([c for c in str(n)])]
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 ''.join(str(s) for s in
list([x, x + 3330, x + (3330 * 2)]
for x in takewhile(lambda n: n < 10000,
(dropwhile(lambda n: n < 1000, prime())))
if isprime(x + 3330) and isprime(x + (3330 * 2))
and set([x + 3330, x + (3330 * 2)])
<= set(combn(x)))[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment