simplify sieving method
This commit is contained in:
parent
f162f99963
commit
5f674230e5
|
@ -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",
|
"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",
|
"\n",
|
||||||
"## Sieving the sums of divisors\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",
|
" dsum = [1 for _ in range(0, limit)]\n",
|
||||||
" \n",
|
" \n",
|
||||||
" for n in range(0, limit):\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",
|
" yield dsum[n]\n",
|
||||||
" continue\n",
|
" continue\n",
|
||||||
" \n",
|
" \n",
|
||||||
" m = 1\n",
|
" for k in range(n, limit, n):\n",
|
||||||
" while m * n < limit:\n",
|
" dsum[k] += n\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",
|
|
||||||
" \n",
|
" \n",
|
||||||
" yield dsum[n]"
|
" yield dsum[n]"
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue