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 / van-eck-sequence / van-eck-sequence.om
| 1 | note Rosetta Code task "Van Eck sequence": https://rosettacode.org/wiki/Van_Eck_sequence |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text first "terms 1 to 10:" |
| 7 | text last "terms 991 to 1000:" |
| 8 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 9 | fn emit a view uchar * to void |
| 10 | o = ulong 0 |
| 11 | while lt o len a bound 4096 |
| 12 | k = write 1 a o sub len a o |
| 13 | if le k 0 |
| 14 | exit 1 |
| 15 | end |
| 16 | o = add o ulong k |
| 17 | end |
| 18 | end |
| 19 | note emitu writes n in decimal; 20 digits hold every ulong |
| 20 | fn emitu n ulong to void |
| 21 | d = new uchar 20 0 |
| 22 | i = ulong 20 |
| 23 | v = n |
| 24 | while true bound 20 |
| 25 | i = sub i 1 |
| 26 | put d i uchar add rem v 10 48 |
| 27 | v = div v 10 |
| 28 | if eq v 0 |
| 29 | break |
| 30 | end |
| 31 | end |
| 32 | emit part d i sub 20 i |
| 33 | end |
| 34 | note a(0) = 0; a(n+1) = n - m where m < n is the last earlier index with a(m) = a(n), else 0 |
| 35 | note ls v is one more than the last index holding v (0: not seen); every term is below 1000, its index |
| 36 | fn main to int |
| 37 | a = new ulong 1000 0 |
| 38 | ls = new ulong 1000 0 |
| 39 | for n ulong 999 |
| 40 | v = get a n |
| 41 | w = get ls v |
| 42 | if gt w 0 |
| 43 | put a add n 1 sub n sub w 1 |
| 44 | end |
| 45 | put ls v add n 1 |
| 46 | end |
| 47 | emit first |
| 48 | for i ulong 10 |
| 49 | emit sp |
| 50 | emitu get a i |
| 51 | end |
| 52 | emit nl |
| 53 | emit last |
| 54 | for i ulong 10 |
| 55 | emit sp |
| 56 | emitu get a add 990 i |
| 57 | end |
| 58 | emit nl |
| 59 | ret 0 |
| 60 | end |