|
发表于 2022-10-25 22:03:22
|
显示全部楼层
本帖最后由 B1tetheDust 于 2022-10-29 15:48 编辑 import time as t
start = t.perf_counter()
def divide_4_digit_num(a_num_list):
new_list = []
for each_num in a_num_list:
new_list.append([str(each_num)[:2], str(each_num)[2:]])
return new_list
def belong_to(a_num):
range_list = [95, 163, 219, 267, 310, 350]
for belong_i in range(6):
if a_num <= range_list[belong_i]:
return belong_i
def find_cyclic_num():
Tri = [int(n * (n + 1) / 2) for n in range(45, 141)] # length = 96
Squ = [int(n ** 2) for n in range(32, 100)] # length = 68
Penta = [int(n * (3 * n - 1) / 2) for n in range(26, 82)] # length = 56
Hexa = [int(n * (2 * n - 1)) for n in range(23, 71)] # length = 48
Hept = [int(n * (5 * n - 3) / 2) for n in range(21, 64)] # length = 43
Octa = [int(n * (3 * n - 2)) for n in range(19, 59)] # length = 40
Tri_list, Squ_list = divide_4_digit_num(Tri), divide_4_digit_num(Squ)
Penta_list, Hexa_list = divide_4_digit_num(Penta), divide_4_digit_num(Hexa)
Hept_list, Octa_list = divide_4_digit_num(Hept), divide_4_digit_num(Octa)
dict_list = {"Tri_list": Tri_list, "Squ_list": Squ_list, "Penta_list": Penta_list, "Hexa_list": Hexa_list,
"Hept_list": Hept_list, "Octa_list": Octa_list}
total = []
for lists in dict_list:
total += dict_list[lists]
len_total = len(total)
for i00 in range(len_total):
index_list = [belong_to(i00)]
for i10 in range(len_total):
if belong_to(i10) not in index_list and total[i00][1] == total[i10][0]:
index_list = [belong_to(i00), belong_to(i10)]
for i20 in range(len_total):
if belong_to(i20) not in index_list and total[i10][1] == total[i20][0]:
index_list = [belong_to(i00), belong_to(i10), belong_to(i20)]
for i30 in range(len_total):
if belong_to(i30) not in index_list and total[i20][1] == total[i30][0]:
index_list = [belong_to(i00), belong_to(i10), belong_to(i20), belong_to(i30)]
for i40 in range(len_total):
if belong_to(i40) not in index_list and total[i30][1] == total[i40][0]:
cur_num_list = [total[i00], total[i10], total[i20], total[i30], total[i40]]
index_list = [belong_to(i00), belong_to(i10), belong_to(i20), belong_to(i30),
belong_to(i40)]
for i50 in range(len_total):
if belong_to(i50) not in index_list and \
total[i40][1] == total[i50][0] and \
total[i50][1] == cur_num_list[0][0]:
cur_num_list.append(total[i50])
return cur_num_list
res = find_cyclic_num()
print(res)
print(sum([int(i[0] + i[1]) for i in res]))
print("It costs %f s" % (t.perf_counter() - start))
[['25', '12'], ['12', '81'], ['81', '28'], ['28', '82'], ['82', '56'], ['56', '25']]
28684
It costs 0.530015 s |
|