Example of how python implements the Fibonacci series

Source: Internet
Author: User
Every coders may write a Fibonacci series in their preferred language. In short, the Fibonacci series start with 0 and 1, the following items are after the first two items. The following article describes how to implement the Fibonacci series in python. if you need it, let's take a look at it. Introduction

The Fibonacci series, also known as the Golden Series, refers to a series of numbers 0, 1, 1, 2, 3, 5, 8, 13, 21 ,...... In mathematics, the Fibonacci series are defined using the following recursive method:

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

1. implementation of tuples

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

This gives you a list of Fibonacci sequences in a 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 + self.b  return self.a  def __iter__(self):  return self

This produces an infinite number of columns, which can be accessed using the following method:

fibs = Fibs()for f in fibs: if f > 1000:  print f  break else:  print f

3. implement through custom classes

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 TypeError("Fib indices must be integers")

In this way, a data structure similar to a sequence can be obtained, and data can be accessed by subscript:

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

4. Python implementation of a simple Fibonacci series example

Let's first release a Fibonacci series...

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

First, assign values to the first two variables:

i, j = 0, 1

Of course, you can also write it like this:

i = 0j = 1

Then set a range, which is within 10000:

while i < 10000:

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

print i,i, j = j, i+j

Note that the code "I, j = I, I + j" cannot be written as follows:

i = jj = i+j

In this case, j is not the sum of the first two values, but the sum of I and j that have been assigned by j. in this case, the output series will be as follows:

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

The correct whole piece of code is as follows:

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

Finally, the running result is displayed:

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


The above is all about the use of Python to implement the Fibonacci series. I hope this article will help you in your study or work. if you have any questions, please leave a message.

For more information about how to use python to implement the Fibonacci series, refer to PHP!

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.