Kayshav.com
About Developer Oracle 11g Technology Information Sitemap

Fibonacci Sequence

-- f(n) = f(n-1) + f(n-2)
-- Where n=1 is 0, n=2 is (1+0) and so on.
-- Use of SIMPLE_INTEGER

SET TIMING ON;
SET PAGESIZE 60;
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
 i_val   SIMPLE_INTEGER := &i_val;
 i_idx   SIMPLE_INTEGER := 0;

FUNCTION fibonacci_seq(i_num IN SIMPLE_INTEGER := i_idx)
   RETURN SIMPLE_INTEGER IS
BEGIN
   IF i_num=1 THEN      RETURN 0;
   ELSIF i_num=2 THEN   RETURN 1;
   ELSE
    RETURN fibonacci_seq(i_num - 1) + fibonacci_seq(i_num - 2);
   END IF;
END;
BEGIN
  IF i_val > 0 THEN
   LOOP
    i_idx := i_idx + 1;
    EXIT WHEN i_idx > i_val;
    DBMS_OUTPUT.PUT_LINE(' Value = '||LPAD(i_idx,5,' ')||
      '  Fibonacci Seq -> '||LPAD(fibonacci_seq(i_idx),10,' '));
   END LOOP;
  ELSE
    DBMS_OUTPUT.PUT_LINE(' Fibonacci Seq starts with 1 ['||i_val||' < 0]');
  END IF;
END;
/

Fibonacci Sequence output


SQL Model - Fibonacci/Hemachandra Sequence

Oracle 11gXEr2 - Index

Oracle registered trademark of Oracle Corporation.

Last Revised On: October 10th, 2014

  39915