mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
Improve solution (#5705)
This commit is contained in:
parent
965b1ff7df
commit
568425dfd1
|
@ -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])
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user