Last active
July 12, 2016 10:50
-
-
Save edib/5906c59f33fc0e5897d4dcbb3052a959 to your computer and use it in GitHub Desktop.
returns fibonacci sequence in pl/pgsql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION fibonacci(num integer) | |
RETURNS SETOF numeric AS $$ | |
DECLARE | |
a numeric := 0; | |
b numeric := 1; | |
BEGIN | |
IF (num <= 0) | |
THEN RETURN; | |
END IF; | |
RETURN NEXT a; | |
LOOP | |
EXIT WHEN num <= 1; | |
RETURN NEXT b; | |
num = num - 1; | |
SELECT b, a + b INTO a, b; | |
END LOOP; | |
END; | |
$$ language plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment