omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / catalan-numbers.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 cc "C(" |
| 8 | text eqs ") = " |
| 9 | text by " by the recurrence, " |
| 10 | text sum " by the convolution sum\n" |
| 11 | text hdr "n, C(n) by C(n+1) = 2(2n+1)/(n+2) C(n), and again by C(n+1) = sum C(i) C(n-i):\n" |
| 12 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 13 | fn emit a view uchar * to void |
| 14 | o = ulong 0 |
| 15 | while lt o len a bound 4096 |
| 16 | k = write 1 a o sub len a o |
| 17 | if le k 0 |
| 18 | exit 1 |
| 19 | end |
| 20 | o = add o ulong k |
| 21 | end |
| 22 | end |
| 23 | note emitu writes n in decimal; 20 digits hold every ulong |
| 24 | fn emitu n ulong to void |
| 25 | d = new uchar 20 0 |
| 26 | i = ulong 20 |
| 27 | v = n |
| 28 | while true bound 20 |
| 29 | i = sub i 1 |
| 30 | put d i uchar add rem v 10 48 |
| 31 | v = div v 10 |
| 32 | if eq v 0 |
| 33 | break |
| 34 | end |
| 35 | end |
| 36 | emit part d i sub 20 i |
| 37 | end |
| 38 | note cs holds C(0)..C(14) from the convolution; all stay below 2^32, so every product fits |
| 39 | fn main to int |
| 40 | cs = new ulong 15 0 |
| 41 | put cs 0 1 |
| 42 | for n ulong 14 |
| 43 | s = ulong 0 |
| 44 | for i ulong add n 1 |
| 45 | s = add s mul get cs i get cs sub n i |
| 46 | end |
| 47 | put cs add n 1 s |
| 48 | end |
| 49 | emit hdr |
| 50 | c = ulong 1 |
| 51 | for n ulong 15 |
| 52 | emit cc |
| 53 | emitu n |
| 54 | emit eqs |
| 55 | emitu c |
| 56 | emit by |
| 57 | emitu get cs n |
| 58 | emit sum |
| 59 | c = div mul c mul 2 add mul 2 n 1 add n 2 |
| 60 | end |
| 61 | ret 0 |
| 62 | end |
| 63 | </syntaxhighlight> |
| 64 | {{out}} |
| 65 | <pre> |
| 66 | n, C(n) by C(n+1) = 2(2n+1)/(n+2) C(n), and again by C(n+1) = sum C(i) C(n-i): |
| 67 | C(0) = 1 by the recurrence, 1 by the convolution sum |
| 68 | C(1) = 1 by the recurrence, 1 by the convolution sum |
| 69 | C(2) = 2 by the recurrence, 2 by the convolution sum |
| 70 | C(3) = 5 by the recurrence, 5 by the convolution sum |
| 71 | C(4) = 14 by the recurrence, 14 by the convolution sum |
| 72 | C(5) = 42 by the recurrence, 42 by the convolution sum |
| 73 | C(6) = 132 by the recurrence, 132 by the convolution sum |
| 74 | C(7) = 429 by the recurrence, 429 by the convolution sum |
| 75 | C(8) = 1430 by the recurrence, 1430 by the convolution sum |
| 76 | C(9) = 4862 by the recurrence, 4862 by the convolution sum |
| 77 | C(10) = 16796 by the recurrence, 16796 by the convolution sum |
| 78 | C(11) = 58786 by the recurrence, 58786 by the convolution sum |
| 79 | C(12) = 208012 by the recurrence, 208012 by the convolution sum |
| 80 | C(13) = 742900 by the recurrence, 742900 by the convolution sum |
| 81 | C(14) = 2674440 by the recurrence, 2674440 by the convolution sum |
| 82 | </pre> |