Skip to content

Instantly share code, notes, and snippets.

@2minchul
Last active February 25, 2019 04:32
Show Gist options
  • Save 2minchul/dbcabcef6596329c7b18bb18d9837049 to your computer and use it in GitHub Desktop.
Save 2minchul/dbcabcef6596329c7b18bb18d9837049 to your computer and use it in GitHub Desktop.
A simple solution for fibonacci number.
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