PY
py
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
from functools import lru_cache
import timeit
# using dict for memoization
my_dict = {}
def q(n):
if n == 1 or n == 2:
my_dict[n] = 1
if n not in my_dict:
my_dict[n] = q(n - q(n-1)) + q(n - (q(n-2)))
return my_dict[n]
# using lru_cache
@lru_cache()
def hf(n):
if n<3:
return 1
return hf(n-hf(n-1))+hf(n-(hf(n-2)))
"""
# using lru_cache on lambda expression
hf = lru_cache(lambda n: n<3 or hf(n-hf(n-1))+hf(n-(hf(n-2))))
"""
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run