Simple Fibonacci Number Class
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]