Created
May 30, 2017 10:26
-
-
Save kishan3/ffb143cba511d675d7b9ac54237b4fc8 to your computer and use it in GitHub Desktop.
Find sum of n fibonacci numbers various methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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