omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / totient-function / oracle.py
| 1 | # Independent reference for "Totient function" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | import math |
| 4 | for n in range(1, 26): |
| 5 | phi = sum(1 for k in range(1, n + 1) if math.gcd(n, k) == 1) |
| 6 | print(f"phi({n}) = {phi}" + (", prime" if phi == n - 1 else "")) |
| 7 | N = 100000 |
| 8 | s = bytearray([1]) * (N + 1) |
| 9 | s[0] = s[1] = 0 |
| 10 | for i in range(2, math.isqrt(N) + 1): |
| 11 | if s[i]: |
| 12 | s[i * i::i] = bytes(len(range(i * i, N + 1, i))) |
| 13 | for lim in [100, 1000, 10000, 100000]: |
| 14 | print(f"primes up to {lim}: {sum(s[:lim + 1])}") |