The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.
Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:
d2d3d4=406 is divisible by 2
d3d4d5=063 is divisible by 3
d4d5d6=635 is divisible by 5
d5d6d7=357 is divisible by 7
d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property.
翻译不高兴贴了
以下是简化后的代码:
check_list = [17, 13, 11, 7, 5, 3, 2]
result = []
t_result = []
def check_multi(number_string):
for s in number_string:
if number_string.count(s) > 1:
return False
return True
for check_number in check_list:
order = check_list.index(check_number)
temp_result = []
for i in range(1, 1000 / check_number):
cur = check_number * i
cur_str = '%03d' % cur
if not check_multi(cur_str):
if order is 0:
temp_result.append(cur)
elif order is 1:
for per in result:
per_str = '%03d' % per
per_f_2 = per_str[:2]
per_b_1 = per_str[2:]
cur_b_2 = cur_str[1:]
if per_f_2 == cur_b_2:
if check_multi(cur_str + per_b_1):
temp_result.append([cur, per])
else:
for per_list in result:
per = per_list[0]
pper = per_list[1]
pper_f_1 = ('%03d' % pper)[:1]
cur_b_1 = cur_str[2:]
if pper_f_1 == cur_b_1:
per_f_2 = ('%03d' % per)[:2]
cur_b_2 = cur_str[1:]
if per_f_2 == cur_b_2:
number_str = '%s' % cur_str
for p in per_list:
number_str += ('%03d' % p)[2:]
if check_multi(number_str):
temp_result.append([cur] + per_list)
result = []
for temp_r in temp_result:
result.append(temp_r)
for r in result:
n_list = [str(i) for i in range(0, 10)]
for number in r:
for s in '%03d' % number:
if s in n_list:
n_list.remove(s)
c = n_list[0]
if not n_list[0] is '0':
c += '%03d' % r[0]
for number in r[1:]:
c += ('%03d' % number)[2:]
t_result.append(eval(c))
print t_result
print sum(t_result)
以下是简化前的代码:
def check_multi(number_string):
for s in number_string:
if number_string.count(s) > 1:
return False
return True
result = {}
final_result = []
final_result_number = []
for i in range(1, 1000 / 17):
result_17 = 17 * i
result_17_str = '%03d' % result_17
if check_multi(result_17_str):
result[result_17_str] = {}
for i in range(1, 1000 / 13):
result_13 = 13 * i
result_13_str = '%03d' % result_13
if check_multi(result_13_str):
result_13_back_2 = result_13_str[1:]
for key_17 in result.keys():
result_17_front_2 = key_17[:2]
if result_13_back_2 == result_17_front_2:
if check_multi(key_17 + result_13_str[0]):
result[key_17]['%03d' % result_13] = {}
for i in range(1, 1000 / 11):
result_11 = 11 * i
result_11_str = '%03d' % result_11
if check_multi(result_11_str):
result_11_back_1 = result_11_str[2:]
for key_17 in result.keys():
result_17_front_1 = key_17[:1]
if result_11_back_1 == result_17_front_1:
for key_13 in result[key_17].keys():
result_11_back_2 = result_11_str[1:]
result_13_front_2 = key_13[:2]
if result_11_back_2 == result_13_front_2:
if check_multi(key_17 + key_13[0] + result_11_str[0]):
result[key_17][key_13]['%03d' % result_11] = {}
for i in range(1, 1000 / 7):
result_7 = 7 * i
result_7_str = '%03d' % result_7
if check_multi(result_7_str):
result_7_back_1 = result_7_str[2:]
for key_17 in result.keys():
for key_13 in result[key_17].keys():
result_13_front_1 = key_13[:1]
if result_7_back_1 == result_13_front_1:
for key_11 in result[key_17][key_13].keys():
result_7_back_2 = result_7_str[1:]
result_11_front_2 = key_11[:2]
if result_7_back_2 == result_11_front_2:
if check_multi(key_17 + key_13[0] + key_11[0] + result_7_str[0]):
result[key_17][key_13][key_11][
'%03d' % result_7] = {}
for i in range(1, 1000 / 5):
result_5 = 5 * i
result_5_str = '%03d' % result_5
if check_multi(result_5_str):
result_5_back_1 = result_5_str[2:]
for key_17 in result.keys():
for key_13 in result[key_17].keys():
for key_11 in result[key_17][key_13].keys():
result_11_front_1 = key_11[:1]
if result_5_back_1 == result_11_front_1:
for key_7 in result[key_17][key_13][key_11].keys():
result_5_back_2 = result_5_str[1:]
result_7_front_2 = key_7[:2]
if result_5_back_2 == result_7_front_2:
if check_multi(key_17 + key_13[0] + key_11[0] + key_7[0] + result_5_str[0]):
result[key_17][key_13][key_11][key_7][
'%03d' % result_5] = {}
for i in range(1, 1000 / 3):
result_3 = 3 * i
result_3_str = '%03d' % result_3
if check_multi(result_3_str):
result_3_back_1 = result_3_str[2:]
for key_17 in result.keys():
for key_13 in result[key_17].keys():
for key_11 in result[key_17][key_13].keys():
for key_7 in result[key_17][key_13][key_11].keys():
for key_5 in result[key_17][key_13][key_11][key_7].keys():
result_7_front_1 = key_7[:1]
if result_3_back_1 == result_7_front_1:
for key_5 in result[key_17][key_13][key_11][key_7].keys():
result_3_back_2 = result_3_str[1:]
result_5_front_2 = key_5[:2]
if result_3_back_2 == result_5_front_2:
if check_multi(key_17 + key_13[0] + key_11[0] + key_7[0] + key_5[0] + result_3_str[0]):
result[key_17][key_13][key_11][key_7][key_5][
'%03d' % result_3] = {}
for i in range(1, 1000 / 2):
result_2 = 2 * i
result_2_str = '%03d' % result_2
if check_multi(result_2_str):
result_2_back_1 = result_2_str[2:]
for key_17 in result.keys():
for key_13 in result[key_17].keys():
for key_11 in result[key_17][key_13].keys():
for key_7 in result[key_17][key_13][key_11].keys():
for key_5 in result[key_17][key_13][key_11][key_7].keys():
for key_3 in result[key_17][key_13][key_11][key_7][key_5].keys():
result_5_front_1 = key_5[:1]
if result_2_back_1 == result_5_front_1:
for key_3 in result[key_17][key_13][key_11][key_7][key_5].keys():
result_2_back_2 = result_2_str[1:]
result_3_front_2 = key_3[:2]
if result_2_back_2 == result_3_front_2:
if check_multi(key_17 + key_13[0] + key_11[0] + key_7[0] + key_5[0] + key_3[0] + result_2_str[0]):
result[key_17][key_13][key_11][key_7][key_5][key_3][
'%03d' % result_2] = {}
for key_17 in result.keys():
for key_13 in result[key_17].keys():
for key_11 in result[key_17][key_13].keys():
for key_7 in result[key_17][key_13][key_11].keys():
for key_5 in result[key_17][key_13][key_11][key_7].keys():
for key_3 in result[key_17][key_13][key_11][key_7][key_5].keys():
for key_2 in result[key_17][key_13][key_11][key_7][key_5][key_3].keys():
print key_2, key_3, key_5, key_7, key_11, key_13, key_17, key_2[0] + key_3[0] + key_5[0] + key_7[0] + key_11[0] + key_13[0] + key_17
final_result.append(
key_2[0] + key_3[0] + key_5[0] + key_7[0] + key_11[0] + key_13[0] + key_17)
for s_result in final_result:
number_list = [str(i) for i in range(0, 10)]
for s in s_result:
number_list.remove(s)
s_result = number_list[0] + s_result
final_result_number.append(eval(s_result))
print sum(final_result_number)
其实我觉得逻辑阅读性还是简化前的比较清楚,虽然实在是太丑陋了