From ee9dc87ab4ca1a61a395e74a42758058d95ff7c0 Mon Sep 17 00:00:00 2001 From: Sidney Zhang Date: Mon, 15 Dec 2025 17:29:36 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(solutions)=EF=BC=9A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=AC=A7=E6=8B=89=E9=A1=B9=E7=9B=AE=E7=AC=AC9?= =?UTF-8?q?=E9=A2=98=E5=92=8C=E7=AC=AC10=E9=A2=98=E7=9A=84=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E6=96=B9=E6=A1=88=20=F0=9F=93=9D=20docs(solutions/000?= =?UTF-8?q?9)=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=95=B0=E5=AD=A6=E6=8E=A8?= =?UTF-8?q?=E5=AF=BC=E8=AF=B4=E6=98=8E=E6=96=87=E6=A1=A3=20=E2=9C=A8=20fea?= =?UTF-8?q?t(solutions/0010)=EF=BC=9A=E5=AE=9E=E7=8E=B0=E7=B4=A0=E6=95=B0?= =?UTF-8?q?=E6=B1=82=E5=92=8C=E7=AE=97=E6=B3=95=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E8=AE=A1=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solutions/0009/readme.md | 7 ++++++ solutions/0010.SumPrimes/eular_10.py | 34 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 solutions/0009/readme.md create mode 100644 solutions/0010.SumPrimes/eular_10.py diff --git a/solutions/0009/readme.md b/solutions/0009/readme.md new file mode 100644 index 0000000..6edfb56 --- /dev/null +++ b/solutions/0009/readme.md @@ -0,0 +1,7 @@ +这个问题从数学上,需要换元替代计算。 + +a = k(m^2 - n^2) +b = 2kmn +c = k(m^2 + n^2) + +基于这个换元进新推导。或者就是直接基于原始命题进行计算,可以参见 [这个解答](https://math.stackexchange.com/a/3378895) 。 diff --git a/solutions/0010.SumPrimes/eular_10.py b/solutions/0010.SumPrimes/eular_10.py new file mode 100644 index 0000000..49a33d0 --- /dev/null +++ b/solutions/0010.SumPrimes/eular_10.py @@ -0,0 +1,34 @@ +""" +Sum of primes below 2 million +""" + +import time + + +def timer(func): + def wrapper(*args, **kwargs): + start = time.time() + result = func(*args, **kwargs) + end = time.time() + print(f"{func.__name__} took {end - start:.6f} seconds") + return result + + return wrapper + + +def sieve_primes(max: int) -> list[int]: + primes = [True] * (max + 1) + primes[0:2] = [False, False] + for i in range(2, max + 1): + if primes[i]: + primes[i * i : max : i] = [False] * ((max - 1 - i * i) // i + 1) + return [i for i in range(max) if primes[i]] + + +@timer +def sum_primes(max: int) -> int: + return sum(sieve_primes(max)) + + +if __name__ == "__main__": + print(sum_primes(2_000_000))