Example of how Python implements the Fibonacci sequence

Source: Internet
Author: User
Introduced

The Fibonacci sequence, also known as the Golden Section, refers to a sequence of numbers 0, 1, 1, 2, 3, 5, 8, 13, and 、...... Mathematically, the Fibonacci sequence is defined in the following recursive way:

F (0) =0,f (1) =1,f (n) =f (n-1) +f (n-2) (n≥2,n∈n*).

1. Meta-Group implementation

Fibs = [0, 1]for I in range (8): Fibs.append (Fibs[-2] + fibs[-1])

This will give you a list of Fibonacci numbers within the specified range.

2. Iterator implementation

Class Fibs:def __init__ (self):  self.a = 0  self.b = 1  def next (self):  self.a, self.b = self.b, self.a + SE lf.b  return SELF.A  def __iter__ (self):  return to self

This will get an infinite number of columns that can be accessed in the following ways:

fibs = fibs () for F in Fibs:if F > £:  print F break  else:  print F

3. By customizing the class implementation

Class Fib (object): Def __getitem__ (self, n):  if Isinstance (n, Int.):   A, B = 1, 1 for   x in range (n):    A, B = b, a + b   return a  elif isinstance (n, slice):   start = N.start   stop = N.stop   A, b = 1, 1   L = []
  
   for x in range (stop):    if x >= start:     l.append (a)    A, B = B, a + b   return L  else:   raise Ty Peerror ("Fib indices must be integers")
  

This gives you a sequence-like data structure that can be used to access data by subscript:

f = Fib () print F[0:5]print f[:10]

4.Python implementation of the simpler Fibonacci sequence example

Let's start with a Fibonacci sequence and see ...

0 1 1 2 3 5 8 13 21 34 55 89 144 233 ...

The first two variables are assigned values:

I, j = 0, 1

Of course, you can also write this:

i = 0J = 1

Then set a range, just 10000 within the good:

While I < 10000:

Then output I in the while statement and design the logic:

Print I,i, j = j, I+j

It is important to note here: "I, j = i, i+j" This code cannot be written as follows:

i = JJ = I+j

If written like this, J is not the sum of the first two bits, but the sum of I and J that has been assigned by J, so the output sequence will look like this:

0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192

The correct whole piece code is as follows:

I, j = 0, 1while i < 10000:print I, I, j = j, I+j

Finally show the results of the operation:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765


Summarize

The above is about the use of Python to achieve the Fibonacci sequence of all the content, I hope that the content of this article on everyone's study or work can bring certain help, if there are questions you can message exchange.

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.