From 745989cb32a20e92f71eb63353c53822192a50ad Mon Sep 17 00:00:00 2001 From: Sidney Zhang Date: Thu, 8 Jan 2026 14:38:53 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(euler=5F37.py)=EF=BC=9A?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dcombine=5Flists=E5=87=BD=E6=95=B0=E5=A4=84?= =?UTF-8?q?=E7=90=86=E7=A9=BA=E5=88=97=E8=A1=A8=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E6=94=B9=E8=BF=9B=E8=BE=93=E5=87=BA=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E6=98=BE=E7=A4=BA=E6=AF=8F=E4=B8=AA=E6=88=AA=E6=96=AD?= =?UTF-8?q?=E7=B4=A0=E6=95=B0=20=F0=9F=93=9D=20docs(0037.TruncatablePrimes?= =?UTF-8?q?)=EF=BC=9A=E6=B7=BB=E5=8A=A0readme.md=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E8=A7=A3=E9=87=8A=E6=88=AA=E6=96=AD=E7=B4=A0=E6=95=B0=E5=8F=AA?= =?UTF-8?q?=E6=9C=8911=E4=B8=AA=E7=9A=84=E5=8E=9F=E5=9B=A0=20=E2=9C=A8=20f?= =?UTF-8?q?eat(euler=5F38.py)=EF=BC=9A=E6=96=B0=E5=A2=9E=E5=85=A8=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E5=80=8D=E6=95=B0=E9=97=AE=E9=A2=98=E7=9A=84=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E6=96=B9=E6=A1=88=EF=BC=8C=E5=8C=85=E5=90=AB=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=AE=97=E6=B3=95=E5=92=8C=E8=AF=A6=E7=BB=86=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solutions/0037.TruncatablePrimes/euler_37.py | 7 +- solutions/0037.TruncatablePrimes/readme.md | 23 +++++ .../0038.PandigitalMultiples/euler_38.py | 84 +++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 solutions/0037.TruncatablePrimes/readme.md create mode 100644 solutions/0038.PandigitalMultiples/euler_38.py diff --git a/solutions/0037.TruncatablePrimes/euler_37.py b/solutions/0037.TruncatablePrimes/euler_37.py index c5799c2..8356fd3 100644 --- a/solutions/0037.TruncatablePrimes/euler_37.py +++ b/solutions/0037.TruncatablePrimes/euler_37.py @@ -26,6 +26,8 @@ def timer(func): def combine_lists(a: list[int], b: list[int | None], c: list[int]) -> list[int]: """将三个列表的每个元素组合成数字""" + if b in [[],[None]] : + return [int(f"{x}{z}") for x, z in product(a, c)] return [int(f"{x}{y}{z}") for x, y, z in product(a, b, c)] @@ -80,7 +82,10 @@ def TruncatablePrime() -> list[int]: @timer def main(): - print(sum(TruncatablePrime())) + nums = TruncatablePrime() + for num in nums: + print(num) + print(f"Answer: {sum(nums)}") if __name__ == "__main__": diff --git a/solutions/0037.TruncatablePrimes/readme.md b/solutions/0037.TruncatablePrimes/readme.md new file mode 100644 index 0000000..8535f46 --- /dev/null +++ b/solutions/0037.TruncatablePrimes/readme.md @@ -0,0 +1,23 @@ +# 截断素数 + +截断素数为什么只有11个? +我看到很多人有这个疑问,我自己也是思考了很久,才恍然的。 + +首先,向左向右分别截断一个素数,得到的依然是素数,这有多难呢?最右的数位显然只能是3、7。 +2、5,会让这个向右截断的数编程可以合数,并且截断到最后只有一位数时,只能是2、3、5、7,在排除前面说的2和5,那么最右的数位只能是3、7。 +相似的向左截断时,最左的数位显然只能是2、3、5、7。 +中间的数,在向左截断时,显然不能时双数,那只能从1、3、5、7、9中选取,而5结尾的数必然可以被5整除,所以中间的数不能有5结尾。 +这也就导致中间只有1、3、7、9这四个数可选。 +到这我们了解了这个最基本的数字约束。 + +其次,由于截断的每一步都是一个素数,这就导致在高位数时可满足上述条件的素数会更为稀疏, +简单尝试验证就能发现,从7位数开始,满足上述约束的截断素数会更少,且不会超过6位数的情况,而6位数只有一个截断素数,739397。 +由于素数的分布是高度不均匀的,所以,而偏向连续位数的素数出现概率本身就会随素数本身的不均匀分布而更难出现。 +这也能很好的解释,为什么没有五位数的截断素数。 + +总之,需要严格证明的话,我们可以根据 [【Angell & Godwin,1977】](https://www.ams.org/journals/mcom/1977-31-137/S0025-5718-1977-0427213-2/S0025-5718-1977-0427213-2.pdf) 简单扩展得到,右截断素数在文章中以证明有83个,且最大的为73939133。那么,就可以简单枚举到这个最大的右截断素数中所有的左截断素数,就是结果了。 + + +----- + +MiroMind的模型真的不错,很好的总结了截断素数相关的论文研究 [【分享链接】](https://dr.miromind.ai/share/e9e297da-7d1b-49d3-b4a9-bdae9249c9aa) 。 diff --git a/solutions/0038.PandigitalMultiples/euler_38.py b/solutions/0038.PandigitalMultiples/euler_38.py new file mode 100644 index 0000000..d484f69 --- /dev/null +++ b/solutions/0038.PandigitalMultiples/euler_38.py @@ -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()