Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/si/rosetta/tasks/hamming-numbers/hamming-numbers.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / hamming-numbers / hamming-numbers.om` 84 lines ยท 1458 bytes Raw Blame History 1 note Rosetta Code task "Hamming numbers": https://rosettacode.org/wiki/Hamming_numbers 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 text tfirst "first 20: " 8 text tnth "1691st: " 9 global ham ulong 1691 10 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 11 fn emit a view uchar * to void 12 o = ulong 0 13 while lt o len a bound 4096 14 k = write 1 a o sub len a o 15 if le k 0 16 exit 1 17 end 18 o = add o ulong k 19 end 20 end 21 note emitu writes n in decimal; 20 digits hold every ulong 22 fn emitu n ulong to void 23 d = new uchar 20 0 24 i = ulong 20 25 v = n 26 while true bound 20 27 i = sub i 1 28 put d i uchar add rem v 10 48 29 v = div v 10 30 if eq v 0 31 break 32 end 33 end 34 emit part d i sub 20 i 35 end 36 note Dijkstra's method: three indices into the list already built point at the smallest numbers whose 37 note double, triple and quintuple are not yet listed; the next number is the least of the three 38 fn min3 a ulong b ulong c ulong to ulong 39 m = a 40 if lt b m 41 m = b 42 end 43 if lt c m 44 m = c 45 end 46 ret m 47 end 48 fn build n ulong to void 49 put ham 0 1 50 i = ulong 0 51 j = ulong 0 52 k = ulong 0 53 for x ulong sub n 1 54 a = mul get ham i 2 55 b = mul get ham j 3 56 c = mul get ham k 5 57 m = min3 a b c 58 put ham add x 1 m 59 if eq a m 60 i = add i 1 61 end 62 if eq b m 63 j = add j 1 64 end 65 if eq c m 66 k = add k 1 67 end 68 end 69 end 70 fn main to int 71 build 1691 72 emit tfirst 73 for i ulong 20 74 if gt i 0 75 emit sp 76 end 77 emitu get ham i 78 end 79 emit nl 80 emit tnth 81 emitu get ham 1690 82 emit nl 83 ret 0 84 end