[Introduction to algorithms-28] Fibonacci series and Their Correlation

Source: Internet
Author: User

1. Solution

The Fibonacci series grew rapidly. The first 100th computers had reached the 10th power, and the 64-bit computers had reached the 19th power. Therefore, the performance is compared by calculating the number of 40th. In fact, the four-byte int type can only calculate the number of 48th ononacci (starting with 0, 1, 1, 2) As 1836 311 903, A number about 1.8 billion.

1) recursive method-Simple Writing and low efficiency

Public class Fibonacci {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo automatically generated method stub long start = system. nanotime (); // obtain the start time int fibonaccino = getfibonacci (40); long end = system. nanotime (); // obtain the end time system. out. println ("program running time:" + (end-Start) + "ns"); system. out. println (fibonaccino);}/* recursive scheme */public static int getfibonacci (int n) {If (n = 1) {return 0 ;} else if (n = 2) {return 1;} else {return getfibonacci (n-1) + getfibonacci (n-2 );}}}
**************************************** **************************

Output: running time: 687841156ns
63245986

2) Dynamic Planning Method-simple writing, extremely efficient, and complex O (N)

Idea: After Class question 370-5 on the 15.1 page of Introduction to algorithms, this is in line with one feature of dynamic planning-the current problem calls two subproblems.

Public class Fibonacci {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo automatically generated method stub long start = system. nanotime (); // get start time int [] fibonaccino = getfibonacci (40); long end = system. nanotime (); // obtain the end time system. out. println ("program running time:" + (end-Start) + "ns"); For (int I: fibonaccino) {system. out. println (I) ;}}/* base-up calculation */public static int [] getfibonacci (INT N) {int [] temp = new int [N]; temp [0] = 0; temp [1] = 1; for (INT I = 2; I <n; I ++) {temp [I] = temp [I-1] + temp [I-2];} return temp ;}}
**************************************** *

Running time: 5366ns
63245986

3) generic formula-opportunistic and inefficient

Idea: 59 pages of Introduction to Algorithms

Public class Fibonacci {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo automatically generated method stub long start = system. nanotime (); // obtain the start time system. out. println (getfibonacci (40); long end = system. nanotime (); // obtain the end time system. out. println ("program running time:" + (end-Start) + "ns");} public static int getfibonacci (int I) {float root = (float) (1 + math. SQRT (5)/2); Return (INT) math. round (math. pow (root, I)/math. SQRT (5 ));}}
**************************************** *

Console output:

102334197

Running time: 655532ns

4) matrix method-complex writing, most efficient, and complex O (lgn)

Reference: http://zhedahht.blog.163.com/blog/static/25411174200722991933440/

2. Related Mathematical Problems

1) Arrange and combine
There are 10 levels of stairs. Each step can only Span one or two levels. How many different steps are there to climb 10th levels?
This is a Fibonacci series: there is a method of boarding the first level; there are two methods of boarding the second level; three methods of boarding the third level; four levels of boarding, there are five login methods ......
1, 2, 3, 5, 8, 13 ...... Therefore, there are 89 steps to attach to the ten levels.
2) the limit of the ratio of the first and second adjacent items in the Series
What is the limit of F (N)/F (n + 1) WHEN n tends to be infinite?
This can be directly obtained from its general formula. The limit is (-1 + √ 5)/2. This is a golden split value and a number representing the harmony of nature.

3. [one interview question]How can I determine whether a number is the number of Fibonacci?(I haven't found the final answer yet)

Forum: http://bbs.csdn.net/topics/120067216

1) when the number is small (less than 20724)

A verynice test is that N is a Fibonacci number if and only if 5 N ^ 2 + 4 or 5N ^ 2-4 is a squarenumber.

That is to say, to determine whether 5 N ^ 2 + 4 or 5N ^ 2-4 is the total number of shards. Here, an algorithm question is raised: "How can we determine whether a number is the total number of shards ?", See http://blog.sina.com.cn/s/blog_5a4882970102dxa5.html. Here we will mainly explain that the number of complete records must be an odd number in a row and: 1 + 3 + 5 + 7 +... + (2 * N-1) = n ^ 2.

Public class Fibonacci {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo automatically generated method stub long start = system. nanotime (); // get start time int [] fibonaccino = getfibonacci (48); long end = system. nanotime (); // obtain the end time system. out. println ("program running time:" + (end-Start) + "ns"); For (int I: fibonaccino) {system. out. print (isfibonaccino (I);}/* whether it is the number of ononacci */public static Boolean isfibonaccino (INT N) {/* 5 N ^ 2 + 4 if it is greater than integer. max_value indicates that the judgment */If (n> math. pow (integer. MAX_VALUE-4)/5, 0.5) {system. out. print ("cannot judge --"); Return false;} int temp = (INT) (5 * Math. pow (n, 2); Return isperfectsquareno (temp + 4) | isperfectsquareno (temp-4);}/* determination of the number of complete records */public static Boolean isperfectsquareno (int n) {for (INT I = 1; n> 0; I + = 2) {n-= I;} return n = 0 ;} /* base-up calculation */public static int [] getfibonacci (int n) {int [] temp = new int [N]; temp [0] = 0; temp [1] = 1; for (INT I = 2; I <n; I ++) {temp [I] = temp [I-1] + temp [I-2];} return temp ;}}
**************************************** *******

The program test shows that the four-byte int type can only be judged to be (INT) math. pow (integer. MAX_VALUE-4)/5, 0.5), about 20724, that is, up to 20 thousand can be judged.

2) When the number is relatively large (the entire int length is determined)

Thought 1

Use the dynamic planning method to generate the Fibonacci series until the generated Fibonacci series is equal to N and returns true. If the number is greater than N, false is returned.

Idea 2

.... To be continued, there should be definitely more efficient algorithms .....








[Introduction to algorithms-28] Fibonacci series and Their Correlation

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.