Skip to content

Instantly share code, notes, and snippets.

@kishan3
Created May 30, 2017 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishan3/ffb143cba511d675d7b9ac54237b4fc8 to your computer and use it in GitHub Desktop.
Save kishan3/ffb143cba511d675d7b9ac54237b4fc8 to your computer and use it in GitHub Desktop.
Find sum of n fibonacci numbers various methods.
class Fibonacci(object):
"""docstring for Fibonacci"""
fib_cache = {}
def fib_memoization(self, n):
if n in fib_cache:
return fib_cache[n]
else:
if n < 2:
fib_cache[n] = n
else:
fib_cache[n] = fib_cache[n-1] + fib_cache[n-2]
return fib_cache[n]
def fib_recursive(self, n):
if n < 2:
return n
return self.fib_recursive(n-1) + self.fib_recursive(n-2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment