Python_ Fibonacci Sequence

Source: Internet
Author: User

What is the Fibonacci sequence?

--a group of values starting from the third value, each equal to an interesting sequence of the sum of the first two values

such as [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

How do I implement it with a program?

--Logical finishing

Initial value n_1 = 1, n_2 = 1

N_3 = n_1 + n_2

The third value starts with each value being the sum of the first two values, if n_1 = n_2, n_2 = N_3,

Implement the overall n_1, n_2 move back a number, that is, now the n_1 +n_2 equivalent to N_2 + n_3

Although the results to n_3 received, but N_3 is essentially the fourth number, it may be the following number is so, Jiaxiang n_3 infinite Backward move

N_3 can replace any subsequent number.

--Programmatic:

List_fab = []                   # definition Receive fab value list Def fab (n):    n_1, n_2 = 1, 1             # define initial value n_1=1,n_2=1    list_fab.append (n_1)        # put two first The starting value is added to the Fab value list    list_fab.append (n_2)    for I in Range (n-2):        # As input requires a few minus 2 because there are two initial values        n_3 = n_1 + n_2         # Third value = The sum of the first two values        list_fab.append (n_3)    # Adds the added value to the FAB value list        n_1, n_2 = n_2, n_3     # n_1 and n_2 synchronously move backward one    return List_fab             # return result = Fab (                #) # Receive results print (List_fab, Len (list_fab))  # Print Results

  This implementation is forward derivation, no sub-problem partitioning-deduced from the back

Python_ Fibonacci Sequence

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.