29 lines
776 B
Text
29 lines
776 B
Text
include builtins\mpfr.e
|
|
|
|
sequence c2cache = {}
|
|
|
|
function catalan2m(integer n) -- very fast!
|
|
object r -- result (a [cached/shared] mpz)
|
|
-- (nb: modifying result will mess up cache)
|
|
if n<=0 then return mpz_init(1) end if
|
|
if n<=length(c2cache) then
|
|
r = c2cache[n]
|
|
if r!=0 then return r end if
|
|
else
|
|
c2cache &= repeat(0,n-length(c2cache))
|
|
end if
|
|
r = mpz_init(0)
|
|
mpz t = mpz_init()
|
|
for i=0 to n-1 do
|
|
mpz_mul(t,catalan2m(i),catalan2m(n-1-i))
|
|
mpz_add(r,r,t)
|
|
end for
|
|
t = mpz_free(t)
|
|
c2cache[n] = r
|
|
return r
|
|
end function
|
|
|
|
sequence s = {}
|
|
for i=0 to 15 do s = append(s,mpz_get_str(catalan2m(i))) end for
|
|
printf(1,"0..15: %s\n",join(s,","))
|
|
printf(1,"100: %s\n",{mpz_get_str(catalan2m(100))})
|