interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / narcissistic-decimal-number / narcissistic-decimal-number.om

73 lines · 1433 bytes Raw Blame History
1note Rosetta Code task "Narcissistic decimal number": https://rosettacode.org/wiki/Narcissistic_decimal_number
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text hdr "first 25 narcissistic decimal numbers:"
7note emit writes every byte of a to stdout; a short write is retried, an error exits 1
8fn emit a view uchar * to void
9o = ulong 0
10while lt o len a bound 4096
11k = write 1 a o sub len a o
12if le k 0
13exit 1
14end
15o = add o ulong k
16end
17end
18note emitu writes n in decimal; 20 digits hold every ulong
19fn emitu n ulong to void
20d = new uchar 20 0
21i = ulong 20
22v = n
23while true bound 20
24i = sub i 1
25put d i uchar add rem v 10 48
26v = div v 10
27if eq v 0
28break
29end
30end
31emit part d i sub 20 i
32end
33note n with k digits is narcissistic when the sum of its digits each raised to k equals n
34note pw holds d^k at 8 * d + k for digits d and lengths k up to 7
35fn narc n ulong pw view ulong 80 to bool
36k = ulong 1
37v = div n 10
38while gt v 0 bound 8
39k = add k 1
40v = div v 10
41end
42s = ulong 0
43v = n
44for i ulong k
45s = add s get pw add mul rem v 10 8 k
46v = div v 10
47end
48ret eq s n
49end
50note bound: the 25th such number, 9926315, has 7 digits, so 10^7 candidates suffice
51fn main to int
52pw = new ulong 80 0
53for d ulong 10
54x = ulong 1
55for k ulong 8
56put pw add mul d 8 k x
57x = mul x d
58end
59end
60emit hdr
61c = ulong 0
62for n ulong 10000000
63if lt c 25
64if narc n pw
65emit sp
66emitu n
67c = add c 1
68end
69end
70end
71emit nl
72ret 0
73end