diff --git a/project_euler/problem_072/sol1.py b/project_euler/problem_072/sol1.py index 846396ab0f9cb2c9cde232148665ff809d7e9dce..a2a0eeeb31c556368af168c213fd1f29db2ca8c1 100644 --- a/project_euler/problem_072/sol1.py +++ b/project_euler/problem_072/sol1.py @@ -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])