Last active
June 3, 2023 09:19
-
-
Save woodRock/fce672cd13d1dcb0ff3e954f2d2bc25c to your computer and use it in GitHub Desktop.
Postgres function to give Fibonacci Sequence in SQL.
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 fib(n int) | |
returns setof int as $$ | |
declare | |
a int := 0; | |
b int := 1; | |
begin | |
return next 0; | |
return next 1; | |
while a < n loop | |
a := a + b; | |
return next a; | |
b := b + a; | |
return next b; | |
end loop; | |
end; | |
$$ language plpgsql; | |
select * from fib(10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment