Skip to content

Instantly share code, notes, and snippets.

@shieldsd
Created April 18, 2012 13:30
Show Gist options
  • Save shieldsd/2413565 to your computer and use it in GitHub Desktop.
Save shieldsd/2413565 to your computer and use it in GitHub Desktop.
Project Euler #41
from itertools import chain
from math import sqrt
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):
l = [c for c in str(n)]
return [int(''.join(x)) for x in comb(l)]
def isprime(n):
for x in range(2, int(sqrt(n)) + 1):
if n % x == 0:
return 0
return 1
print max(x for x in
(chain(*[combn(int('123456789'[:i]))
for i in range(1, 8)])) if isprime(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment