-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path41.py
37 lines (30 loc) · 800 Bytes
/
41.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import itertools
def is_prime(n):
if n == 1 or n == 0: return False
if n == 2 or n == 3: return True
i = 3
while i <= n**0.5:
if not n%i: return False
i += 2
return True
all_units = [1,3,7,9]
def pandigit_prime(n):
units = [u for u in all_units if u <= n]
best = 0
powers = [10**i for i in range(1,n)]
for u in units:
ran = range(1,n+1)
ran.remove(u)
for p in itertools.permutations(ran):
can = u+sum([a*b for a,b in zip(p, powers)])
if can > best:
if is_prime(can):
best = can
return best
def problem41():
best = 0
for n in range(1, 9):
c = pandigit_prime(n)
if c: best = c
return best
print problem41()