diff --git a/project_euler/problem_072/sol2.py b/project_euler/problem_072/sol2.py new file mode 100644 index 000000000000..08e92c18bb3a --- /dev/null +++ b/project_euler/problem_072/sol2.py @@ -0,0 +1,45 @@ +""" +Project Euler Problem 72: https://projecteuler.net/problem=72 + +Consider the fraction, n/d, where n and d are positive integers. If n int: + """ + Return the number of reduced proper fractions with denominator less than limit. + >>> solution(8) + 21 + >>> solution(1000) + 304191 + """ + primes = set(range(3, limit, 2)) + primes.add(2) + for p in range(3, limit, 2): + if p not in primes: + continue + primes.difference_update(set(range(p * p, limit, p))) + + phi = [float(n) for n in range(limit + 1)] + + for p in primes: + for n in range(p, limit + 1, p): + phi[n] *= 1 - 1 / p + + return int(sum(phi[2:])) + + +if __name__ == "__main__": + print(f"{solution() = }")