interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / hamming-numbers / hamming-numbers.om

84 lines · 1458 bytes Raw Blame History
1note Rosetta Code task "Hamming numbers": https://rosettacode.org/wiki/Hamming_numbers
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tfirst "first 20: "
8text tnth "1691st: "
9global ham ulong 1691
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note Dijkstra's method: three indices into the list already built point at the smallest numbers whose
37note double, triple and quintuple are not yet listed; the next number is the least of the three
38fn min3 a ulong b ulong c ulong to ulong
39m = a
40if lt b m
41m = b
42end
43if lt c m
44m = c
45end
46ret m
47end
48fn build n ulong to void
49put ham 0 1
50i = ulong 0
51j = ulong 0
52k = ulong 0
53for x ulong sub n 1
54a = mul get ham i 2
55b = mul get ham j 3
56c = mul get ham k 5
57m = min3 a b c
58put ham add x 1 m
59if eq a m
60i = add i 1
61end
62if eq b m
63j = add j 1
64end
65if eq c m
66k = add k 1
67end
68end
69end
70fn main to int
71build 1691
72emit tfirst
73for i ulong 20
74if gt i 0
75emit sp
76end
77emitu get ham i
78end
79emit nl
80emit tnth
81emitu get ham 1690
82emit nl
83ret 0
84end