I’m trying to generate the Fibonacci sequence with a function in SQL. It takes an input parameter pstop integer
(10 by default) and returns a table with all Fibonacci numbers less than pstop
.
But the output starts with these numbers (1, 2, 3, 5, 8, 13) and skips the numbers of the beginning of the sequence which are 0 and 1 (0, 1, 2, 3, 5, 8, 13).
How can I fix it?
CREATE OR REPLACE FUNCTION fnc_fibonacci (pstop INTEGER DEFAULT 10)
RETURNS TABLE (fibonacci_num INTEGER) AS $BODY$
DECLARE
a INTEGER;
b INTEGER;
c INTEGER;
BEGIN
a := 0;
b := 1;
fibonacci_num := 0;
WHILE
fibonacci_num < pstop LOOP
c := a + b;
fibonacci_num := c;
IF
fibonacci_num < pstop THEN
RETURN NEXT;
END IF;
a := b;
b := c;
END LOOP;
END;
$BODY$
LANGUAGE PLPGSQL;
SELECT * FROM fnc_fibonacci(20);
2
Answers
To note: the Fibonacci sequence starts with 0, 1, 1, 2 (not 0, 1, 2).
Assignments are ever so slightly expensive in PL/pgSQL. So keep those at a minimum. It’s an academic consideration for a function returning no more than 46 rows with type
integer
. (It gets more relevant with big numbers operating withnumeric
.)Anyway, here is an optimized function with a single addition and assignment per output row:
fiddle
Note that we can simply
RETURN NEXT 0;
in a function declared withRETURNS SETOF int
without naming anyOUT
parameters. (Column names inRETURNS TABLE
areOUT
parameters, too.)Details in the manual chapter "Returning from a Function".