Fibonacci Rides Again… and Again

Suppose my previous Fibonacci code should be a function. Here it is:

CREATE OR REPLACE FUNCTION fibonacci(intRETURNS SETOF int AS
$BODY$
WITH RECURSIVE t(a, b) AS (
    VALUES (11)
UNION ALL
    SELECT b, a + b FROM t
)
SELECT a FROM t  LIMIT $1;
$BODY$
IMMUTABLE
STRICT
LANGUAGE SQL;

Take care!

2 thoughts on “Fibonacci Rides Again… and Again

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s