interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / combinations.wiki

94 lines · 1664 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text tcount "combinations: "
10global comb ulong 8
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37note nextc steps comb[0..k-1] to the next k-subset of 0..n-1 in lexicographic order: raise the last
38note element that can still rise and reset the ones after it; false after the last subset
39fn nextc k ulong n ulong to bool
40i = k
41while gt i 0 bound 8
42i = sub i 1
43if lt get comb i add sub n k i
44put comb i add get comb i 1
45for x ulong sub sub k i 1
46j = add add i x 1
47put comb j add get comb sub j 1 1
48end
49ret true
50end
51end
52ret false
53end
54fn show k ulong to void
55for i ulong k
56if gt i 0
57emit sp
58end
59emitu get comb i
60end
61emit nl
62end
63note 5 choose 3 is 10, which bounds the loop
64fn main to int
65k = ulong 3
66for i ulong k
67put comb i i
68end
69c = ulong 1
70show k
71while nextc k 5 bound 10
72show k
73c = add c 1
74end
75emit tcount
76emitu c
77emit nl
78ret 0
79end
80</syntaxhighlight>
81{{out}}
82<pre>
830 1 2
840 1 3
850 1 4
860 2 3
870 2 4
880 3 4
891 2 3
901 2 4
911 3 4
922 3 4
93combinations: 10
94</pre>