Return the number of reduced proper fractions with denominator less than limit. >>> solution(8) 21 >>> solution(1000) 304191
(limit: int = 1000000)
| 18 | |
| 19 | |
| 20 | def 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 | |
| 44 | if __name__ == "__main__": |