Last active
February 25, 2019 04:32
-
-
Save 2minchul/dbcabcef6596329c7b18bb18d9837049 to your computer and use it in GitHub Desktop.
A simple solution for fibonacci number.
This file contains 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
from functools import lru_cache | |
@lru_cache(maxsize=None) # caching all them, do not discard cache | |
def fibonacci_dynamic_recursive(n): | |
if n == 0 or n == 1: | |
return n | |
return fibonacci_dynamic_recursive(n - 1) + fibonacci_dynamic_recursive(n - 2) | |
def fibonacci(n): | |
for _n in range(100, n, 100): # to prevent too many stack memory usage | |
fibonacci_dynamic_recursive(_n) | |
return fibonacci_dynamic_recursive(n) | |
if __name__ == '__main__': | |
print(fibonacci(10000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment