|
发表于 2022-10-29 17:54:36
|
显示全部楼层
- import time as t
- start = t.perf_counter()
- class P62:
- def __init__(self, test_range):
- self.test_range = test_range
- self.cubic_pool = [i ** 3 for i in range(200, test_range)]
- self.bool_num = [True] * (test_range - 345)
- self.count = 1
- self.num_list = []
- self.find_nums()
- @staticmethod
- def is_permutable(num_1, num_2):
- str_num_1, str_num_2 = str(num_1), str(num_2)
- if len(str_num_1) != len(str_num_2):
- return False
- for each_digit in str_num_1:
- if each_digit not in str_num_2:
- return False
- else:
- str_num_2 = str_num_2.replace(each_digit, '', 1)
- return True
- def check_num(self, num_1):
- test_num = num_1 + 1
- while test_num < self.test_range:
- if self.bool_num[test_num - 345] and \
- self.is_permutable(self.cubic_pool[num_1 - 200], self.cubic_pool[test_num - 200]):
- self.count += 1
- self.num_list.append(test_num)
- self.bool_num[test_num - 345] = False
- test_num += 1
- def find_nums(self):
- for num0 in range(345, self.test_range):
- if self.bool_num[num0 - 345]:
- self.num_list = [num0]
- self.count = 1
- self.check_num(num0)
- if self.count == 5:
- print(self.num_list)
- print([self.cubic_pool[j - 200] for j in self.num_list])
- break
- res = P62(10000)
- print("It costs %f s" % (t.perf_counter() - start))
复制代码
[5027, 7061, 7202, 8288, 8384]
[127035954683, 352045367981, 373559126408, 569310543872, 589323567104]
It costs 23.116533 s |
|