omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / narcissistic-decimal-number / oracle.py
| 1 | # Independent reference for "Narcissistic decimal number" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | # by digit multisets: for each length k and each sorted choice of k digits, the power sum is |
| 4 | # narcissistic when its own digits, sorted, are that choice |
| 5 | import itertools |
| 6 | found = set() |
| 7 | for k in range(1, 8): |
| 8 | for combo in itertools.combinations_with_replacement(range(10), k): |
| 9 | s = sum(d ** k for d in combo) |
| 10 | if len(str(s)) == k and tuple(sorted(map(int, str(s)))) == combo: |
| 11 | found.add(s) |
| 12 | found.add(0) |
| 13 | print("first 25 narcissistic decimal numbers: " + " ".join(map(str, sorted(found)[:25]))) |