import time time.clock() def check_3(result, num, num_set): for p in num_set: if 0 == int(p + num[:2]) % 3: last_num = ''.join(list(num_set - set(p))) result.append(int(last_num + p + num)) result.append(int(last_num[::-1] + p + num)) def check_5(result, num, num_set): for p in num_set: if 0 == int(p) % 2: check_3(result, p + num, num_set - set(p)) def check_7(result, num, num_set): for p in num_set: if 0 == int(p + num[:2]) % 7: check_5(result, p + num, num_set - set(p)) def check_11(result, num, num_set): for p in '05': if 0 == int(p + num[:2]) % 11: check_7(result, p + num, num_set - set(p)) def check_13(result, num, num_set): for p in num_set: if '0' == p or '5' == p: continue if 0 == int(p + num[:2]) % 13: check_11(result, p + num, num_set - set(p)) def check_17(): result = [] for n in range(136, 1000, 17): num = str(n) if '0' in num or '5' in num: continue if len(num) == len(set(num)): check_13(result, num, set('1234567890') - set(num)) return result print(sum(check_17())) print(time.clock())