def str2num(n):
return int(''.join(map(str, n)))
from itertools import permutations
thelist = []
for first in permutations(range(10), 4):
firstpiece = first
firstnumber = str2num(first)
if firstnumber%2 == 0:
thelist.append(firstpiece)
primes = [3, 5, 7, 11, 13, 17]
for p in primes:
newlist = thelist
thelist = []
for new in newlist:
# second = str(second)
#print new
for i in range(10):
#i = str(i)
if i not in new:
newpiece = list(new); newpiece.append(i)
#print newpiece
newnumber = str2num(newpiece[-3:])
if newnumber%p == 0:
thelist.append((newpiece))
final = map(str2num, thelist)
print final
print sum(final)