Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active November 14, 2020 13:16
Show Gist options
  • Save mGalarnyk/7ee805df0cc0a44a45ebaab1cb7cf3d4 to your computer and use it in GitHub Desktop.
Save mGalarnyk/7ee805df0cc0a44a45ebaab1cb7cf3d4 to your computer and use it in GitHub Desktop.
Fibonacci sequence algorithm in Python. 5 different ways for a later blog post at https://medium.com/@GalarnykMichael
# To incorporate and learn from later: http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python
##########################################
# Method 1: Simple For Loops
# If you like, you can specify which Python version you are using
# Python 2 Version
# (xrange doesnt exist in Python3)
a, b = 0, 1
for i in xrange(0, 10):
print a
a, b = b, a + b
# Python 3 Version Differences in print statements
a, b = 0, 1
for i in range(0, 10):
print(a)
a,b = b, a + b
############################################
# Method 2: Generators
def fib(num):
a,b = 0, 1
for i in xrange(0, num):
yield "{}:: {}".format(i + 1,a)
a, b = b, a + b
for item in fib(10):
print item
# Method 3: Recursion
# https://medium.com/developers-writing/fibonacci-sequence-algorithm-in-javascript-b253dc7e320e#.yataqtu90
# Mention how inefficient this is
# Method 4: Memoization
# Method 5 Object Oriented Way Challenge Question.
'''Fibonacci Sequence Object
write a class Fibonacci whose constructor takes two numbers; the class uses these two numbers as the first two numbers in the sequence.
The class should have a method calculate(n) that returns the n-th number in the sequence.
Add a method next(). The first call to next() returns the first number in the sequence, the second call returns the second number, and so on. You'll need instance variables to save state between calls.
Finally, add a method writeToFile(n, filename), that writes the first n numbers in the sequence to the file named filename, one number per line.
HINT: you should store previously computed fibonachi numbers instead of recomputing every one as they are needed
'''
# Solution:
class Fibonacci:
def __init__(self):
self.i = 0
self.x = 0
self.y = 1
self.F = [self.x,self.y]
def next(self):
self.i +=1
return self.F[self.i-1] # return next # from list F
def calculate(self, n):
# use previously computed fibonachi numbers stored in list F
l = len(self.F)
x = self.F[l-2]
y = self.F[l-1]
newn = n-l
# calculate new numbers if it does not exist in list
for i in range(newn+1):
x, y = y, x + y
self.F.append(y)
return self.F[n] # return nth # in Fibonacci series
def writeToFile(self, n, filename):
filehandle = open(filename, 'a') #append to file
self.calculate(n)
for j in range(n):
filehandle.write(str(self.F[j])+'\n')
filehandle.close()
@andy-shev
Copy link

There is interesting method with help of a formula. See https://stackoverflow.com/a/499245/2511795 for the details.

Copy link

ghost commented Feb 9, 2020

Or you can try:
n=int(input('Until where?'))
sequence=[0,1]
for i in range(0,n):
an = sequence[i] + sequence[i + 1]
sequence.append(an)
print(sequence)

@violistt
Copy link

Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment