feat(euler_37.py):修复combine_lists函数处理空列表的情况,并改进输出格式显示每个截断素数

📝 docs(0037.TruncatablePrimes):添加readme.md文档解释截断素数只有11个的原因
 feat(euler_38.py):新增全数字倍数问题的解决方案,包含优化算法和详细注释
This commit is contained in:
2026-01-08 14:38:53 +08:00
parent 5b1a9f0e4e
commit 745989cb32
3 changed files with 113 additions and 1 deletions

View File

@@ -0,0 +1,84 @@
"""
Take the number 192 and multiply it by each of 1, 2, and 3:
192 * 1 = 192
192 * 2 = 384
192 * 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576.
We will call 192384576 the concatenated product of 192 and (1,2,3).
The same can be achieved by starting with 9 and multiplying by 1,2,3,4, and 5,
giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).
What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product
of an integer with (1,2,...,n) where n > 1?
"""
import time
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"Execution time: {end_time - start_time:.6f} seconds")
return result
return wrapper
# def max_pandiMulti():
# max_num = 0
# key = [1,2,3,4,5]
# res = (0, [])
# ok = False
# for i in range(9999, 9, -1):
# mul_keys = [i * j for j in key]
# for j in range(5) :
# numx = "".join(map(str, mul_keys[:j+1]))
# if len(numx) == 9 and set(numx) == set('123456789'):
# max_num = max(max_num, int(numx))
# if max_num == int(numx):
# res = (i, key[:j+1])
# ok = True
# break
# if ok:
# break
# return max_num, res
def max_pandiMulti_better():
max_num = 0
res = (0, [])
# 配置列表:(n, start, end),其中 start > end表示从大到小遍历
configs = [
(2, 9999, 5000), # n=2: i ∈ [5000, 9999]
(3, 333, 100), # n=3: i ∈ [100, 333]
(4, 33, 25), # n=4: i ∈ [25, 33]
(5, 9, 5) # n=5: i ∈ [5, 9]
]
for n, start, end in configs:
for i in range(start, end - 1, -1):
# 生成拼接字符串
s = ''.join(str(i * k) for k in range(1, n + 1))
# 检查是否为1-9 pandigital
if set(s) == set('123456789'):
num = int(s)
if num > max_num:
max_num = num
res = (i, list(range(1, n + 1)))
break # 当前n中i递减第一个满足条件的即为最大可终止内层循环
return max_num, res
@timer
def main():
maxnum, ress = max_pandiMulti_better()
print(f"{maxnum} : {ress}")
if __name__ == "__main__":
main()