Oracle implements a series of fibonacci

Source: Internet
Author: User

Method 1 for Oracle to implement the fibonacci series:

Select replace (MAX (SYS_CONNECT_BY_PATH (fib | ',', '/'), '/', '') | '...' fiblist
FROM (
SELECT n, fib, ROW_NUMBER ()
OVER (order by n) r
FROM (select n, round (1 + sqrt (5) * 0.5, n)-power (1-sqrt (5) * 0.5, n )) /sqrt (5) fib
From (select level n
From dual
Connect by level <= 16) t1) t2
)
Start with r = 1
Connect by prior r = R-1;
/*
FIBLIST
--------------------------------------------------------------------------------
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144,233,377,610,987 ,...
*/


Method 2:

DECLARE
A number;
B NUMBER;
C number;
BEGIN
A: = 0;
B: = 1;
C: = 1;
FOR I IN 1 .. 20 LOOP
DBMS_OUTPUT.PUT_LINE ('the '| I | 'Number is:' | C );
C: = A + B;
A: = B;
B: = C;
End loop;
END;
/*
The 1 number is: 1
The 2 number is: 1
The 3 number is: 2
The 4 number is: 3
The 5 number is: 5
The 6 number is: 8
The 7 number is: 13
The 8 number is: 21
The 9 number is: 34
The 10 number is: 55
The 11 number is: 89
The 12 number is 144
The 13 number is: 233
The 14 number is: 377
The 15 number is: 610
The 16 number is: 987
The 17 number is: 1597
The 18 number is: 2584
The 19 number is: 4181
The 20 number is: 6765
*/

Method 3:

Select max (s) | ',...' ononacci_list
From
(Select s
From dual
Model
Return all rows
Dimension by (0 d)
Measures (cast (''as varchar2 (200) s, 0 f)
Rules iterate (16)
(F [iteration_number] = decode (iteration_number, 0, 1, 1, 1, f [iteration_number-1] + f [iteration_number-2]),
S [iteration_number] = decode (iteration_number, 0, to_char (f [iteration_number]), s [iteration_number-1] | ',' | to_char (f [iteration_number])
)
)
/*
FIBONACCI_LIST
--------------------------------------------------------------------------------
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144,233,377,610,987 ,...
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.