Improve solution (#5705)

This commit is contained in:
Maxim Smolskiy 2021-10-31 13:37:46 +03:00 committed by GitHub
parent 965b1ff7df
commit 568425dfd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 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. 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)] phi = [i - 1 for i in range(limit + 1)]
for i in range(2, limit + 1): for i in range(2, limit + 1):
if phi[i] == i - 1:
for j in range(2 * i, limit + 1, i): for j in range(2 * i, limit + 1, i):
phi[j] -= phi[i] phi[j] -= phi[j] // i
return sum(phi[2 : limit + 1]) return sum(phi[2 : limit + 1])