The python implementation of the Fibonacci sequence (Fibonacci)

Source: Internet
Author: User
Tags for in range

First: Using a For loop

The use of the For loop, does not involve functions, but this method for my small white is a good understanding of the function of a more abstract ...

1 >>> fibs = [0,1]2 for in range (8):3         Fibs.append (Fibs[-2] + fibs[-1])45 >>> fibs6 [0, 1, 1, 2 , 3, 5, 8, 13, 21, 34]

Or, enter a dynamic length:

1fibs = [0,1]2num  = input ('howmany Fibonacci numbers does you want? ' )3 for in range (Num-2):4     fibs.append (Fibs[-2] + Fibs[-1])5     print (fibs)

The second: the use of functions

Function 1:

1 def fibs (num): 2     result = [0,1]3for in      Range (num-2):4         Result.append (Result[-2] + result[-1])5     return  result6  7 >>> fibs8 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Function 2:

1 def fibs (n): 2     A, B = 0,13      while a < N:4         print  (a)5 A,         B = b,a+b

Or the following form:

def fibs (max):     = 0,0,1 while     n < Max:        print  (a)        = b,a+b        = n + 1    return'done'

By adding a max, you can make the number of additions consistent with the incoming parameter max, for example, the input 10,a+b is added 10 times and then the loop is ended. In the case of Max, there is only one while a < n, so the loop ends when you go to a < N, because A is constantly becoming B, so it certainly doesn't increase by 1 each time.

Function 3:

1 def fibs (n): 2     0,13     result = []4while      a < N:5          result.append (b)6a b         = b,a + b7     return result

Function 2 and function 3 is almost, function 2 is each additional number to print out, function 3 is added to the result of each additional number, the final output result.

Function 4: Using recursion

1 def Fab (n): 2   if n==1:3     return 14   if n==0:5      return  06   else:7     Result =int (Fab (n-1)) +int (Fab (n-2))    8     return result

Following the refinement code:

1 def fibs (n): 2     if or n = = 1:3         return 14     else  :5          return fibs (n-1) + fibs (n-2)

The first glimpse of Python code is brief. But I'm a little dizzy with the return of the hand.

The test code is as follows:

1  for  in range:2   print fibs (i)
  

The python implementation of the Fibonacci sequence (Fibonacci)

Related Article

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.