Skip to content

Commit 568425d

Browse files
Improve solution (TheAlgorithms#5705)
1 parent 965b1ff commit 568425d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

project_euler/problem_072/sol1.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
function, phi(n). So, the answer is simply the sum of phi(n) for 2 <= n <= 1,000,000
1919
Sum of phi(d), for all d|n = n. This result can be used to find phi(n) using a sieve.
2020
21-
Time: 3.5 sec
21+
Time: 1 sec
2222
"""
2323

2424

@@ -36,8 +36,9 @@ def solution(limit: int = 1_000_000) -> int:
3636
phi = [i - 1 for i in range(limit + 1)]
3737

3838
for i in range(2, limit + 1):
39-
for j in range(2 * i, limit + 1, i):
40-
phi[j] -= phi[i]
39+
if phi[i] == i - 1:
40+
for j in range(2 * i, limit + 1, i):
41+
phi[j] -= phi[j] // i
4142

4243
return sum(phi[2 : limit + 1])
4344

0 commit comments

Comments
 (0)