omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / mobius-function / oracle.py
| 1 | # Independent reference for "Möbius function" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | def mu(n): |
| 4 | f, d, m = 0, 2, n |
| 5 | while d * d <= m: |
| 6 | if m % d == 0: |
| 7 | m //= d |
| 8 | if m % d == 0: |
| 9 | return 0 |
| 10 | f += 1 |
| 11 | d += 1 |
| 12 | if m > 1: |
| 13 | f += 1 |
| 14 | return -1 if f % 2 else 1 |
| 15 | print("first 199 terms of the Moebius sequence (the first cell, n = 0, is blank):") |
| 16 | for r in range(10): |
| 17 | print("".join(" " if n == 0 else f"{mu(n):3d}" for n in range(20 * r, 20 * r + 20))) |