omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / combinations.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 tcount "combinations: " |
| 10 | global comb ulong 8 |
| 11 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 12 | fn emit a view uchar * to void |
| 13 | o = ulong 0 |
| 14 | while lt o len a bound 4096 |
| 15 | k = write 1 a o sub len a o |
| 16 | if le k 0 |
| 17 | exit 1 |
| 18 | end |
| 19 | o = add o ulong k |
| 20 | end |
| 21 | end |
| 22 | note emitu writes n in decimal; 20 digits hold every ulong |
| 23 | fn emitu n ulong to void |
| 24 | d = new uchar 20 0 |
| 25 | i = ulong 20 |
| 26 | v = n |
| 27 | while true bound 20 |
| 28 | i = sub i 1 |
| 29 | put d i uchar add rem v 10 48 |
| 30 | v = div v 10 |
| 31 | if eq v 0 |
| 32 | break |
| 33 | end |
| 34 | end |
| 35 | emit part d i sub 20 i |
| 36 | end |
| 37 | note nextc steps comb[0..k-1] to the next k-subset of 0..n-1 in lexicographic order: raise the last |
| 38 | note element that can still rise and reset the ones after it; false after the last subset |
| 39 | fn nextc k ulong n ulong to bool |
| 40 | i = k |
| 41 | while gt i 0 bound 8 |
| 42 | i = sub i 1 |
| 43 | if lt get comb i add sub n k i |
| 44 | put comb i add get comb i 1 |
| 45 | for x ulong sub sub k i 1 |
| 46 | j = add add i x 1 |
| 47 | put comb j add get comb sub j 1 1 |
| 48 | end |
| 49 | ret true |
| 50 | end |
| 51 | end |
| 52 | ret false |
| 53 | end |
| 54 | fn show k ulong to void |
| 55 | for i ulong k |
| 56 | if gt i 0 |
| 57 | emit sp |
| 58 | end |
| 59 | emitu get comb i |
| 60 | end |
| 61 | emit nl |
| 62 | end |
| 63 | note 5 choose 3 is 10, which bounds the loop |
| 64 | fn main to int |
| 65 | k = ulong 3 |
| 66 | for i ulong k |
| 67 | put comb i i |
| 68 | end |
| 69 | c = ulong 1 |
| 70 | show k |
| 71 | while nextc k 5 bound 10 |
| 72 | show k |
| 73 | c = add c 1 |
| 74 | end |
| 75 | emit tcount |
| 76 | emitu c |
| 77 | emit nl |
| 78 | ret 0 |
| 79 | end |
| 80 | </syntaxhighlight> |
| 81 | {{out}} |
| 82 | <pre> |
| 83 | 0 1 2 |
| 84 | 0 1 3 |
| 85 | 0 1 4 |
| 86 | 0 2 3 |
| 87 | 0 2 4 |
| 88 | 0 3 4 |
| 89 | 1 2 3 |
| 90 | 1 2 4 |
| 91 | 1 3 4 |
| 92 | 2 3 4 |
| 93 | combinations: 10 |
| 94 | </pre> |