RosettaCodeData/Task/Catalan-numbers/DuckDB/catalan-numbers.duckdb
2025-08-11 18:05:26 -07:00

30 lines
666 B
Text

# table of the first Catalan numbers up to catalan(n)
# using the DuckDB type determined by `one`, which should be
# 1 of an appropriate type:
create or replace function catalan_t_(mx, one) as table (
with recursive cte(n,c) as (
select 0 as n, one as c
union all
select n+1,
2 * (2 * n + 1) * c / (n+2)
from cte
where n < mx
)
from cte
);
# In general, use UHUGEINT
create or replace function catalan_t(mx) as table (
from catalan_t_(mx, 1::UHUGEINT)
);
# point-wise
create or replace function catalan(nn) as (
select c
from catalan_t(nn)
offset nn
);
from catalan_t(15);
from catalan_t_(100, 1::DOUBLE) offset 100;