From 5f674230e5c4e6487b92236e4cc3bb28858333b1 Mon Sep 17 00:00:00 2001 From: filifa Date: Sat, 19 Jul 2025 21:08:12 -0400 Subject: [PATCH] simplify sieving method --- notebooks/problem0021.ipynb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/notebooks/problem0021.ipynb b/notebooks/problem0021.ipynb index e87c765..1ab28b6 100644 --- a/notebooks/problem0021.ipynb +++ b/notebooks/problem0021.ipynb @@ -68,7 +68,7 @@ "Therefore, if you have the number's factorization (see [problem 3](https://projecteuler.net/problem=3)), you can use it to compute the sum of its divisors.\n", "\n", "## Sieving the sums of divisors\n", - "Since we need all the sums of divisors up to 10000, instead of factoring each number individually, we could sieve the values of $\\sigma$. This is possible because the sum of divisors function is multiplicative." + "Since we need all the sums of divisors up to 10000, instead of factoring each number individually, we could sieve the values of $\\sigma$." ] }, { @@ -82,18 +82,12 @@ " dsum = [1 for _ in range(0, limit)]\n", " \n", " for n in range(0, limit):\n", - " if n == 0 or n == 1 or dsum[n] != 1:\n", + " if n == 0 or n == 1:\n", " yield dsum[n]\n", " continue\n", " \n", - " m = 1\n", - " while m * n < limit:\n", - " dsum[m*n] = dsum[m] + m*n\n", - " for k in range(2 * m * n, limit, m * n):\n", - " if k % (m*n^2) != 0:\n", - " dsum[k] *= dsum[m*n]\n", - " \n", - " m *= n\n", + " for k in range(n, limit, n):\n", + " dsum[k] += n\n", " \n", " yield dsum[n]" ]