未验证 提交 568425df 编写于 作者: M Maxim Smolskiy 提交者: GitHub

Improve solution (#5705)

上级 965b1ff7
......@@ -18,7 +18,7 @@ Number of numbers between 1 and n that are coprime to n is given by the Euler's
function, phi(n). So, the answer is simply the sum of phi(n) for 2 <= n <= 1,000,000
Sum of phi(d), for all d|n = n. This result can be used to find phi(n) using a sieve.
Time: 3.5 sec
Time: 1 sec
"""
......@@ -36,8 +36,9 @@ def solution(limit: int = 1_000_000) -> int:
phi = [i - 1 for i in range(limit + 1)]
for i in range(2, limit + 1):
for j in range(2 * i, limit + 1, i):
phi[j] -= phi[i]
if phi[i] == i - 1:
for j in range(2 * i, limit + 1, i):
phi[j] -= phi[j] // i
return sum(phi[2 : limit + 1])
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册