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

27 lines
812 B
Text

# The specification of `zero`, `one` and `incr` will determine precision.
# For maximum precision, specify them as UHUGEINT.
create or replace function Leonardo_table(max, zero, one, incr) as table (
with recursive cte(n,lp,l) as (
select 1 as n, zero as lp, one as l
union all
select n+1, l as lp, l + lp + incr as l
from cte
where n<max
)
select 0::UHUGEINT as n, 1::UHUGEINT as l
union all
select n, l
from cte
);
create or replace function Leonardo(nn, zero, one, incr) as (
select last(l order by n) from Leonardo_table(nn, zero, one, incr)
);
.print Leonardo_table(25,1,1,1);
from Leonardo_table(25,1,1,1);
.print Leonardo_table(25,0::UHUGEINT,1::UHUGEINT,0);
from Leonardo_table(25,0::UHUGEINT,1::UHUGEINT,0);
select Leonardo(91,1::UHUGEINT,1::UHUGEINT,1::UHUGEINT);