PL/SQL 中的斐波那契數程式設計


給定數字‘n’,任務是在 PL/SQL 中生成斐波那契數,從 0 到 n,其中整數斐波那契數列的形式為

0, 1, 1, 2, 3, 5, 8, 13, 21, 34

其中,數字 0 和 1 有固定的間隔,之後新增兩個數字,例如:

0+1=1(3rd place)
1+1=2(4th place)
2+1=3(5th place) and So on

斐波那契數列的序列 F(n) 的遞推關係定義為 −

Fn = Fn-1 + Fn-2
Where, F(0)=0 and F(1)=1 are always fixed

PL/SQL 是 Oracle 產品,是 SQL 和過程語言 (PL) 的組合,在 90 年代初推出。它被引入以向簡單的 SQL 新增更多特性和功能。

示例

   -- declare variable a = 0 for first digit in a series
   -- declare variable b = 0 for Second digit in a series
   -- declare variable temp for first digit in a series
declare
a number := 0;
b number := 1;
temp number;
n number := 10;
i number;
begin
   dbms_output.put_line('fibonacci series is :');
   dbms_output.put_line(a);
   dbms_output.put_line(b);
   for i in 2..n
   loop
      temp:= a + b;
      a := b;
      b := temp;
      dbms_output.put_line(temp);
   end loop;
end;

輸出

fibonacci series is : 0 1 1 2 3 5 8 13 21 34

更新於: 20-09-2019

10K+ 瀏覽量

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告