RosettaCodeData/Task/Fibonacci-sequence/DuckDB/fibonacci-sequence-4.duckdb

17 lines
506 B
Plaintext

# Warning: the following program has been naively adapted from the PostgresQL but
# should be further modified to ensure correctness, e.g. by adding a counter
CREATE or replace FUNCTION fib(n) AS (
WITH RECURSIVE fibonacci(current, previous) AS (
SELECT 0::UHUGEINT, 1::UHUGEINT
-- another possibility: SELECT 0::FLOAT, 1::FLOAT
UNION ALL
SELECT previous + current, current FROM fibonacci
)
SELECT current FROM fibonacci
LIMIT 1 OFFSET n
);
# Example
select fib(100);