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