Oracle: Fibonacci Series by SQL

You can use phi to compute the nth number in the Fibonacci series (fn).
If you consider 0 in the Fibonacci series to correspond to n = 0, use this formula:
fn =  Phi n / 5½

Query to generate 10 fibonacci series numbers
SELECT ROUND(POWER(1.6180339,LEVEL) / POWER(5,0.5)) FROM DUAL CONNECT BY LEVEL < =10

The value of Phi can be calculated as
(1 + sqrt (5))/2 = 1.61803399

So we can rewriet our query as
SELECT ROUND(POWER((1 + sqrt (5))/2,LEVEL) / POWER(5,0.5)) FROM DUAL CONNECT BY LEVEL < =10


We can also generate Fibonacci Series by Recursive Sub-Query factoring
with data(a, b, c) as (
    select 0 a, 1 b, 10 from dual
    union all
    select b, a+b, c-1 from data where c >= 0
)
select  a from  data;


Related Links
ORA-01436: CONNECT BY loop in user data
http://nimishgarg.blogspot.in/2011/02/ora-01436-connect-by-loop-in-user-data.html

Connect By Prior (Hierarchical Query)
http://nimishgarg.blogspot.in/2010/02/oracle-connect-by-prior-hierarchical.html

Get All Month or Week Days Names
http://nimishgarg.blogspot.in/2010/01/oracle-sql-get-all-month-names-jan-to.html

Alpha Numeric Counter Or Sequence
http://nimishgarg.blogspot.in/2011/04/alpha-numeric-counter-or-sequence.html

Oracle SQL: Triangular Series (1 3 6 10 15 21)
http://nimishgarg.blogspot.in/2010/07/oracle-sql-triangular-series-1-3-6-10.html

5 comments: