Files
SolutionEuler/solutions/0037.TruncatablePrimes/euler_37.py
Sidney Zhang 09ddf3f65c feat(main.py):重构主函数调用方式,将 app() 调用封装到 main() 函数中
 feat(euler_37.py):添加截断质数问题的初始实现
 feat(euler_37_primeclass.py):添加基于质数生成器的截断质数完整解决方案
📝 docs(millerrabin_test.pdf):添加 Miller-Rabin 测试的详细文档
2026-01-07 18:26:00 +08:00

44 lines
1.3 KiB
Python

"""
The number 3797 has an interesting property.
Being prime itself, it is possible to continuously remove digits from left to right,
and remain prime at each stage: 3797, 797, 97, and 7.
Similarly we can work from right to left: 3797, 379, 37, and 3.
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
"""
from itertools import product
def combine_lists(a: list[int], b: list[int|None], c: list[int]) -> list[int]:
"""将三个列表的每个元素组合成数字"""
return [int(f"{x}{y}{z}") for x, y, z in product(a, b, c)]
def is_prime(n: int) -> bool:
"""判断一个数是否为素数"""
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
def TruncatablePrime() -> list[int]:
begin = [2, 3, 5, 7]
end = [3, 7]
middle = [1, 3, 7, 9]
res = []
for length in range(2, 7):
if length - 2 == 0:
midb = []
else:
midb = product(middle, repeat=length - 2)
midb = [int("".join(map(str, x))) for x in midb]
nums = combine_lists(begin, midb, end)
for num in nums :
if is