Skip to content

Commit 8905e62

Browse files
committed
Improve fibonacci caching
1 parent 134e683 commit 8905e62

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
cache = {}
2+
13
def fibonacci(n):
4+
if n in cache:
5+
return cache[n]
6+
27
if n <= 1:
3-
return n
4-
return fibonacci(n - 1) + fibonacci(n - 2)
8+
result = n
9+
else:
10+
result = fibonacci(n - 1) + fibonacci(n - 2)
11+
12+
cache[n] = result
13+
return result

0 commit comments

Comments
 (0)