MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_072/sol2.py:20–41  ·  view source on GitHub ↗

Return the number of reduced proper fractions with denominator less than limit. >>> solution(8) 21 >>> solution(1000) 304191

(limit: int = 1000000)

Source from the content-addressed store, hash-verified

18
19
20def solution(limit: int = 1000000) -> int:
21 """
22 Return the number of reduced proper fractions with denominator less than limit.
23 >>> solution(8)
24 21
25 >>> solution(1000)
26 304191
27 """
28 primes = set(range(3, limit, 2))
29 primes.add(2)
30 for p in range(3, limit, 2):
31 if p not in primes:
32 continue
33 primes.difference_update(set(range(p * p, limit, p)))
34
35 phi = [float(n) for n in range(limit + 1)]
36
37 for p in primes:
38 for n in range(p, limit + 1, p):
39 phi[n] *= 1 - 1 / p
40
41 return int(sum(phi[2:]))
42
43
44if __name__ == "__main__":

Callers 1

sol2.pyFile · 0.70

Calls 1

addMethod · 0.45

Tested by

no test coverage detected