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