interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / combinations / combinations.om

77 lines · 1451 bytes Raw Blame History
1note Rosetta Code task "Combinations": https://rosettacode.org/wiki/Combinations
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tcount "combinations: "
8global comb ulong 8
9note emit writes every byte of a to stdout; a short write is retried, an error exits 1
10fn emit a view uchar * to void
11o = ulong 0
12while lt o len a bound 4096
13k = write 1 a o sub len a o
14if le k 0
15exit 1
16end
17o = add o ulong k
18end
19end
20note emitu writes n in decimal; 20 digits hold every ulong
21fn emitu n ulong to void
22d = new uchar 20 0
23i = ulong 20
24v = n
25while true bound 20
26i = sub i 1
27put d i uchar add rem v 10 48
28v = div v 10
29if eq v 0
30break
31end
32end
33emit part d i sub 20 i
34end
35note nextc steps comb[0..k-1] to the next k-subset of 0..n-1 in lexicographic order: raise the last
36note element that can still rise and reset the ones after it; false after the last subset
37fn nextc k ulong n ulong to bool
38i = k
39while gt i 0 bound 8
40i = sub i 1
41if lt get comb i add sub n k i
42put comb i add get comb i 1
43for x ulong sub sub k i 1
44j = add add i x 1
45put comb j add get comb sub j 1 1
46end
47ret true
48end
49end
50ret false
51end
52fn show k ulong to void
53for i ulong k
54if gt i 0
55emit sp
56end
57emitu get comb i
58end
59emit nl
60end
61note 5 choose 3 is 10, which bounds the loop
62fn main to int
63k = ulong 3
64for i ulong k
65put comb i i
66end
67c = ulong 1
68show k
69while nextc k 5 bound 10
70show k
71c = add c 1
72end
73emit tcount
74emitu c
75emit nl
76ret 0
77end