The Simpipo series of interesting deeds

Source: Internet
Author: User

The first to study this sequence is, of course, Fibonacci. He was to describe the number of rabbits growing as follows.

650) this.width=650; "src=" Http:// "title=" 1.PNG " alt= "Wkiol1ciwvndtjbgaalehbfruhc174.png"/>

Later, it is widely used in various occasions, this is the definition of the following columns:

650) this.width=650; "src=" Http:// "title=" 2.PNG " alt= "Wkiol1cixcrhpuaraajx79jyrni439.png"/>

First of all, when we look at this sequence, we think of the recursive method of realizing it first:

650) this.width=650; "src=" Http:// "style=" float: none; "title=" 3.PNG "alt=" Wkiom1cixkxxwurhaaazkba0kcw693.png "/>

can also be implemented using the three-mesh operator:

650) this.width=650; "src=" Http:// "style=" float: none; "title=" 4.PNG "alt=" Wkiol1cixvai-4bhaaalcdrncpi729.png "/>


Recursive time complexity: number of recursion times * per recursive count.

Recursive spatial complexity: recursive depth * the size of each recursive.

Using recursion to implement Fibonacci sequences is very inefficient.

The time complexity is O (2^n), and the spatial complexity is O (n).

Optimization of the Fibonacci sequence:

Fibonacci Sequence: 0,1,1,2,3,5,8 ...

The law can be drawn: Starting with the third number, each number is the sum of the first two numbers.

650) this.width=650; "src=" Http:// "style=" float: none; "title=" 5.PNG "alt=" Wkiom1cixyfwuzi6aaaaojh610k940.png "/>

Note: The time complexity is O (n), the space complexity is O (1)

It can also be implemented by using arrays:

650) this.width=650; "src=" Http:// "style=" float: none; "title=" 6.PNG "alt=" Wkiom1cixyeqwddjaabfo_axxuo609.png "/>

Note: The time is complex O (n) and the spatial complexity is O (n).


(1) in the Fibonacci sequence, be sure to note that when n=0, the result is 0.

(2) Apply a long long to prevent cross-border.

This article from "Together to see the Stars" blog, reproduced please contact the author!

The Simpipo series of interesting deeds

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: 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.