omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / hamming-numbers.wiki
| 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 (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text minus "-" |
| 9 | text tfirst "first 20: " |
| 10 | text tnth "1691st: " |
| 11 | global ham ulong 1691 |
| 12 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 13 | fn emit a view uchar * to void |
| 14 | o = ulong 0 |
| 15 | while lt o len a bound 4096 |
| 16 | k = write 1 a o sub len a o |
| 17 | if le k 0 |
| 18 | exit 1 |
| 19 | end |
| 20 | o = add o ulong k |
| 21 | end |
| 22 | end |
| 23 | note emitu writes n in decimal; 20 digits hold every ulong |
| 24 | fn emitu n ulong to void |
| 25 | d = new uchar 20 0 |
| 26 | i = ulong 20 |
| 27 | v = n |
| 28 | while true bound 20 |
| 29 | i = sub i 1 |
| 30 | put d i uchar add rem v 10 48 |
| 31 | v = div v 10 |
| 32 | if eq v 0 |
| 33 | break |
| 34 | end |
| 35 | end |
| 36 | emit part d i sub 20 i |
| 37 | end |
| 38 | note Dijkstra's method: three indices into the list already built point at the smallest numbers whose |
| 39 | note double, triple and quintuple are not yet listed; the next number is the least of the three |
| 40 | fn min3 a ulong b ulong c ulong to ulong |
| 41 | m = a |
| 42 | if lt b m |
| 43 | m = b |
| 44 | end |
| 45 | if lt c m |
| 46 | m = c |
| 47 | end |
| 48 | ret m |
| 49 | end |
| 50 | fn build n ulong to void |
| 51 | put ham 0 1 |
| 52 | i = ulong 0 |
| 53 | j = ulong 0 |
| 54 | k = ulong 0 |
| 55 | for x ulong sub n 1 |
| 56 | a = mul get ham i 2 |
| 57 | b = mul get ham j 3 |
| 58 | c = mul get ham k 5 |
| 59 | m = min3 a b c |
| 60 | put ham add x 1 m |
| 61 | if eq a m |
| 62 | i = add i 1 |
| 63 | end |
| 64 | if eq b m |
| 65 | j = add j 1 |
| 66 | end |
| 67 | if eq c m |
| 68 | k = add k 1 |
| 69 | end |
| 70 | end |
| 71 | end |
| 72 | fn main to int |
| 73 | build 1691 |
| 74 | emit tfirst |
| 75 | for i ulong 20 |
| 76 | if gt i 0 |
| 77 | emit sp |
| 78 | end |
| 79 | emitu get ham i |
| 80 | end |
| 81 | emit nl |
| 82 | emit tnth |
| 83 | emitu get ham 1690 |
| 84 | emit nl |
| 85 | ret 0 |
| 86 | end |
| 87 | </syntaxhighlight> |
| 88 | {{out}} |
| 89 | <pre> |
| 90 | first 20: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 |
| 91 | 1691st: 2125764000 |
| 92 | </pre> |