Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/narcissistic-decimal-number.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / narcissistic-decimal-number.wiki`
80 lines ยท 1686 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text hdr "first 25 narcissistic decimal numbers:"
9 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
10 fn emit a view uchar * to void
11 o = ulong 0
12 while lt o len a bound 4096
13 k = write 1 a o sub len a o
14 if le k 0
15 exit 1
16 end
17 o = add o ulong k
18 end
19 end
20 note emitu writes n in decimal; 20 digits hold every ulong
21 fn emitu n ulong to void
22 d = new uchar 20 0
23 i = ulong 20
24 v = n
25 while true bound 20
26 i = sub i 1
27 put d i uchar add rem v 10 48
28 v = div v 10
29 if eq v 0
30 break
31 end
32 end
33 emit part d i sub 20 i
34 end
35 note n with k digits is narcissistic when the sum of its digits each raised to k equals n
36 note pw holds d^k at 8 * d + k for digits d and lengths k up to 7
37 fn narc n ulong pw view ulong 80 to bool
38 k = ulong 1
39 v = div n 10
40 while gt v 0 bound 8
41 k = add k 1
42 v = div v 10
43 end
44 s = ulong 0
45 v = n
46 for i ulong k
47 s = add s get pw add mul rem v 10 8 k
48 v = div v 10
49 end
50 ret eq s n
51 end
52 note bound: the 25th such number, `9926315`, has 7 digits, so 10^7 candidates suffice
53 fn main to int
54 pw = new ulong 80 0
55 for d ulong 10
56 x = ulong 1
57 for k ulong 8
58 put pw add mul d 8 k x
59 x = mul x d
60 end
61 end
62 emit hdr
63 c = ulong 0
64 for n ulong `10000000`
65 if lt c 25
66 if narc n pw
67 emit sp
68 emitu n
69 c = add c 1
70 end
71 end
72 end
73 emit nl
74 ret 0
75 end
76
77 {{out}}
78
79 first 25 narcissistic decimal numbers: 0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 `1741725` `4210818` `9800817` `9926315`
80