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-with-repetitions.wiki

103 lines · 1884 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 t0 "iced"
10text t1 "jam"
11text t2 "plain"
12text tcount "choices of 3 from 10 kinds: "
13global pick ulong 4
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitu writes n in decimal; 20 digits hold every ulong
26fn emitu n ulong to void
27d = new uchar 20 0
28i = ulong 20
29v = n
30while true bound 20
31i = sub i 1
32put d i uchar add rem v 10 48
33v = div v 10
34if eq v 0
35break
36end
37end
38emit part d i sub 20 i
39end
40note nextm steps pick[0..k-1], a non-decreasing list of kinds 0..n-1, to the next one in lexicographic
41note order: raise the last pick below n-1 and set every pick after it to the same kind
42fn nextm k ulong n ulong to bool
43i = k
44while gt i 0 bound 4
45i = sub i 1
46if lt get pick i sub n 1
47v = add get pick i 1
48for x ulong sub k i
49put pick add i x v
50end
51ret true
52end
53end
54ret false
55end
56fn kind d ulong to void
57if eq d 0
58emit t0
59end
60if eq d 1
61emit t1
62end
63if eq d 2
64emit t2
65end
66end
67fn main to int
68note the two-doughnut choices from three kinds: C(3+2-1, 2) = 6
69put pick 0 0
70put pick 1 0
71while true bound 6
72kind get pick 0
73emit sp
74kind get pick 1
75emit nl
76if not nextm 2 3
77break
78end
79end
80note counted, not computed: C(10+3-1, 3) = 220 choices bound the loop
81put pick 0 0
82put pick 1 0
83put pick 2 0
84c = ulong 1
85while nextm 3 10 bound 220
86c = add c 1
87end
88emit tcount
89emitu c
90emit nl
91ret 0
92end
93</syntaxhighlight>
94{{out}}
95<pre>
96iced iced
97iced jam
98iced plain
99jam jam
100jam plain
101plain plain
102choices of 3 from 10 kinds: 220
103</pre>