problem43
import itertools def is_ok(ns): for i, x in zip(range(1, 9), (2, 3, 5, 7, 11, 13, 17)): if not ((100*ns[i]+10*ns[i+1]+ns[i+2]) % x == 0): return False return True cnt = 0 for ns in itertools.permutations(range(10), 10): if is_ok(ns): cnt += reduce(lambda r, x : 10*r+x, ns, 0) print(cnt)