interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / catalan-numbers / catalan-numbers.om

60 lines · 1239 bytes Raw Blame History
1note Rosetta Code task "Catalan numbers": https://rosettacode.org/wiki/Catalan_numbers
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text cc "C("
6text eqs ") = "
7text by " by the recurrence, "
8text sum " by the convolution sum\n"
9text 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"
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note cs holds C(0)..C(14) from the convolution; all stay below 2^32, so every product fits
37fn main to int
38cs = new ulong 15 0
39put cs 0 1
40for n ulong 14
41s = ulong 0
42for i ulong add n 1
43s = add s mul get cs i get cs sub n i
44end
45put cs add n 1 s
46end
47emit hdr
48c = ulong 1
49for n ulong 15
50emit cc
51emitu n
52emit eqs
53emitu c
54emit by
55emitu get cs n
56emit sum
57c = div mul c mul 2 add mul 2 n 1 add n 2
58end
59ret 0
60end