Ad
Code
Diff
  • from functools import lru_cache
    
    @lru_cache(maxsize=None)
    def fibonacci(b):
        if 0 <= b <= 1:
            return b
        elif b < 0:
            return None
        return fibonacci(b-1) + fibonacci(b-2)
    • def fibonacci(b, cache={}):
    • if b <= 1:
    • from functools import lru_cache
    • @lru_cache(maxsize=None)
    • def fibonacci(b):
    • if 0 <= b <= 1:
    • return b
    • if b in cache:
    • return cache[b]
    • cache[b] = fibonacci(b-1, cache) + fibonacci(b-2, cache)
    • return cache[b]
    • elif b < 0:
    • return None
    • return fibonacci(b-1) + fibonacci(b-2)