omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / perfect-totient-numbers / oracle.py
| 1 | # Independent reference for "Perfect totient numbers" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | def phi(n): |
| 4 | r, m, p = n, n, 2 |
| 5 | while p * p <= m: |
| 6 | if m % p == 0: |
| 7 | while m % p == 0: |
| 8 | m //= p |
| 9 | r -= r // p |
| 10 | p += 1 |
| 11 | if m > 1: |
| 12 | r -= r // m |
| 13 | return r |
| 14 | out, n = [], 1 |
| 15 | while len(out) < 20: |
| 16 | n += 1 |
| 17 | s, v = 0, n |
| 18 | while v != 1: |
| 19 | v = phi(v) |
| 20 | s += v |
| 21 | if s == n: |
| 22 | out.append(n) |
| 23 | print("first 20 perfect totient numbers: " + " ".join(map(str, out))) |