interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

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}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text hdr "first 25 narcissistic decimal numbers:"
9note emit writes every byte of a to stdout; a short write is retried, an error exits 1
10fn emit a view uchar * to void
11o = ulong 0
12while lt o len a bound 4096
13k = write 1 a o sub len a o
14if le k 0
15exit 1
16end
17o = add o ulong k
18end
19end
20note emitu writes n in decimal; 20 digits hold every ulong
21fn emitu n ulong to void
22d = new uchar 20 0
23i = ulong 20
24v = n
25while true bound 20
26i = sub i 1
27put d i uchar add rem v 10 48
28v = div v 10
29if eq v 0
30break
31end
32end
33emit part d i sub 20 i
34end
35note n with k digits is narcissistic when the sum of its digits each raised to k equals n
36note pw holds d^k at 8 * d + k for digits d and lengths k up to 7
37fn narc n ulong pw view ulong 80 to bool
38k = ulong 1
39v = div n 10
40while gt v 0 bound 8
41k = add k 1
42v = div v 10
43end
44s = ulong 0
45v = n
46for i ulong k
47s = add s get pw add mul rem v 10 8 k
48v = div v 10
49end
50ret eq s n
51end
52note bound: the 25th such number, 9926315, has 7 digits, so 10^7 candidates suffice
53fn main to int
54pw = new ulong 80 0
55for d ulong 10
56x = ulong 1
57for k ulong 8
58put pw add mul d 8 k x
59x = mul x d
60end
61end
62emit hdr
63c = ulong 0
64for n ulong 10000000
65if lt c 25
66if narc n pw
67emit sp
68emitu n
69c = add c 1
70end
71end
72end
73emit nl
74ret 0
75end
76</syntaxhighlight>
77{{out}}
78<pre>
79first 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</pre>