DEV Community

Discussion on: Challenge: Write the recursive Fibonacci algorithm in a different language.

Collapse
 
scottishross profile image
Ross Henderson

Here it is in Oracle SQL:

with FIBONACCI (i, SPIRAL, PREV) as 
(
   select 
      1 i, 0 SPIRAL, cast(null as number) PREV 
   from 
      DUAL
   union all
   select 
      f1.i + 1 i, f1.SPIRAL + nvl(f1.PREV,1) SPIRAL, f1.SPIRAL PREV
    from 
       FIBONACCI f1
    where 
   f1.i < 605
)

select SPIRAL from FIBONACCI order by i;

The limit of 605 is the limit before numeric overflow.