-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathfibonacci.py
More file actions
44 lines (31 loc) · 932 Bytes
/
fibonacci.py
File metadata and controls
44 lines (31 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: UTF-8 -*-
#
# Numeric Algorithms: Factorials and Memoization
# The All ▲lgorithms library for python
#
# Contributed by: Chance
# Github: @chance-nelson
#
def fibonacci(n):
"""Compute the Fibonacci sequence number at a certain index
This is the obvious recursive approach, and runs on the order of O(n*2).
"""
# Base case 1: fibonacci(1) == 0
if n == 1:
return 0
# Base case 2: fibonacci(2) == 1
elif n == 2:
return 1
return fibonacci(n-1) + fibonacci(n-2)
def memoize(f):
"""Special decorator to add memoization to any recursive function
Keep a cache of results from past runs. This cache can then be referenced
instead of calling the function again.
"""
memory = {}
def inner(num):
if num not in memory:
memory[num] = f(num)
return memory[num]
return inner
fibonacci_memo = memoize(fibonacci)