Simple Fibonacci Number Class

chandramde 19 June, 2010 11:53 Electrical Permalink Trackbacks (0)

This simple class of Fibonacci number generator is offering a faster function. Instead of using an iteration, function uses a simple addition operator. Enjoy...

#************************************************************
# Fibonacci Class
# Offering faster and stack friendly non-iterative function
#
# Chandra MDE - chandramde@telkom.net
# Electric Python
#************************************************************
 
class Fibonacci:
    def __init__(self):
        self.firstten = (1,1,2,3,5,8,13,21,34,55)
 
    def fib(self, n):
        if n<=10:
            return self.firstten[n-1]
        else:
            self.lfib = list(self.firstten)
            for i in range(10, n):
                self.lfib.append(self.lfib[-2]+self.lfib[-1])
            return self.lfib[-1]
 
    def fiblist(self, n):
        if n<10:
            return self.firstten[:n]
        elif n==10:
            return self.firstten
        else:
            self.fib(n)
            return self.lfib
 
# Example
F = Fibonacci()
print "First ten of Fibonacci number:", F.firstten
print "The twentieth of Fibonacci number:", F.fib(50)
print "The first twenty of Fibonacci:", F.fiblist(15)
 
 
 

The result:

First ten of Fibonacci number: (1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
The twentieth of Fibonacci number: 12586269025
The first twenty of Fibonacci: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]


Comments

  1. I can attest that your headline "electric python simple fibonacci number class..." font size does not fully formed on my web browser, (I just want to let you know) but perhaps it's my windows 98 ie browser that is causing a issue otherwise that you've got a hot topic

    books get over break up

    get over your ex boyfriend

    .

    Posted by cant get over my ex — 13 Aug 2010, 18:28

  2. Your headline keyword "electric python simple fibonacci number class..." is just good, other webmaster should take it from you.

    how get over relationship advice

    get over an ex girlfriend

    Posted by cant get over ex — 06 Sep 2010, 19:40


Add comment

Add comment
 authimage

Powered by LifeType
© 2006 - Design by Omar Romero (all rights reserved)