From e5868c26497859adb9a2c4709f240bf73cf208a0 Mon Sep 17 00:00:00 2001 From: Sidney Zhang Date: Tue, 3 Feb 2026 14:35:55 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(0046.GoldbachsConjecture)?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=AC=A7=E6=8B=89=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=AC=AC46=E9=A2=98=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88=20?= =?UTF-8?q?=F0=9F=93=9D=20docs(0046.GoldbachsConjecture)=EF=BC=9A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=97=AE=E9=A2=98=E6=8F=8F=E8=BF=B0=E5=92=8C=E8=A7=A3?= =?UTF-8?q?=E9=A2=98=E6=80=9D=E8=B7=AF=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0046.GoldbachsConjecture/euler_46.py | 81 +++++++++++++++++++ solutions/0046.GoldbachsConjecture/readme.md | 4 + 2 files changed, 85 insertions(+) create mode 100644 solutions/0046.GoldbachsConjecture/euler_46.py create mode 100644 solutions/0046.GoldbachsConjecture/readme.md diff --git a/solutions/0046.GoldbachsConjecture/euler_46.py b/solutions/0046.GoldbachsConjecture/euler_46.py new file mode 100644 index 0000000..b898fe4 --- /dev/null +++ b/solutions/0046.GoldbachsConjecture/euler_46.py @@ -0,0 +1,81 @@ +""" +It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. + +9 = 7 + 2 * 1^2 +15 = 7 + 2 * 2^2 +21 = 3 + 2 * 3^2 +25 = 7 + 2 * 3^2 +27 = 19 + 2 * 2^2 +33 = 31 + 2 * 1^2 + +It turns out that the conjecture was false. +What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? +""" + +import math +import time + + +def timer(func): + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + print(f"{func.__name__} took {end_time - start_time:.6f} seconds") + return result + + return wrapper + + +def sieve_of_eratosthenes(limit: int = 5000) -> list[bool]: + """生成一个布尔数组,is_prime[i]为True表示i是质数""" + is_prime = [True] * (limit + 1) + is_prime[0:2] = [False, False] + + for i in range(2, int(limit**0.5) + 1): + if is_prime[i]: + for j in range(i * i, limit + 1, i): + is_prime[j] = False + return is_prime + + +def optimized_find_counterexamples(limit: int = 10000) -> list[int]: + """ + 优化版本,使用集合提高查找效率 + """ + # 生成质数表 + is_prime = sieve_of_eratosthenes(limit) + primes = [i for i in range(limit + 1) if is_prime[i]] + + # 预计算所有奇合数 + odd_composites = set(n for n in range(3, limit + 1, 2) if not is_prime[n]) + + # 预计算2k²的值 + max_k = int(math.sqrt(limit / 2)) + 1 + two_k_squared = [2 * k * k for k in range(1, max_k + 1)] + + # 计算所有可以表示的奇合数 + representable_odd_composites = set() + for p in primes: + if p > limit: + break + for tks in two_k_squared: + n = p + tks + if n > limit: + break + if n in odd_composites: + representable_odd_composites.add(n) + + # 找出不能表示的奇合数 + return sorted(odd_composites - representable_odd_composites) + + +@timer +def main(limit: int = 20000) -> None: + res = optimized_find_counterexamples(limit) + for r in res: + print(r) + + +if __name__ == "__main__": + main() diff --git a/solutions/0046.GoldbachsConjecture/readme.md b/solutions/0046.GoldbachsConjecture/readme.md new file mode 100644 index 0000000..6786e2c --- /dev/null +++ b/solutions/0046.GoldbachsConjecture/readme.md @@ -0,0 +1,4 @@ +这个问题,怎么都不算有简洁的方式找到,暴力搜索是唯一途径,但所幸,这个搜索还算快速。 + +另外,这个问题可以在 [OEIS](https://oeis.org/A060003) 上直接搜索到。 +这也算是最简单偷懒的方式了吧。