Returns an integer, the solution to the problem >>> solution(10) 31 >>> solution(100) 3043 >>> solution(1_000) 304191
(limit: int = 1_000_000)
| 25 | |
| 26 | |
| 27 | def solution(limit: int = 1_000_000) -> int: |
| 28 | """ |
| 29 | Returns an integer, the solution to the problem |
| 30 | >>> solution(10) |
| 31 | 31 |
| 32 | >>> solution(100) |
| 33 | 3043 |
| 34 | >>> solution(1_000) |
| 35 | 304191 |
| 36 | """ |
| 37 | |
| 38 | # generating an array from -1 to limit |
| 39 | phi = np.arange(-1, limit) |
| 40 | |
| 41 | for i in range(2, limit + 1): |
| 42 | if phi[i] == i - 1: |
| 43 | ind = np.arange(2 * i, limit + 1, i) # indexes for selection |
| 44 | phi[ind] -= phi[ind] // i |
| 45 | |
| 46 | return int(np.sum(phi[2 : limit + 1])) |
| 47 | |
| 48 | |
| 49 | if __name__ == "__main__": |