-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathackermann_cached.py
39 lines (34 loc) · 1.16 KB
/
ackermann_cached.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
29
30
31
32
33
34
35
36
37
38
39
import sys
sys.setrecursionlimit(2000) # You really can't set it much higher than this without crashing, but even this isn't good enough for (4, 3)
ACKERMANN_CACHE = {}
def ackermannWithCache(m, n, indentation=None):
if (m, n) in ACKERMANN_CACHE:
return ACKERMANN_CACHE[(m ,n)]
if indentation is None:
indentation = 0
print('%sackermannWithCache(%s, %s)' % (' ' * indentation, m, n))
if m == 0:
# BASE CASE
result = n + 1
ACKERMANN_CACHE[(m, n)] = result
return result
elif m > 0 and n == 0:
# RECURSIVE CASE
result = ackermannWithCache(m - 1, 1, indentation + 1)
ACKERMANN_CACHE[(m, n)] = result
return result
elif m > 0 and n > 0:
# RECURSIVE CASE
result = ackermannWithCache(m - 1, ackermannWithCache(m, n - 1, indentation + 1), indentation + 1)
ACKERMANN_CACHE[(m, n)] = result
return result
"""
print('Starting with m = 1, n = 1:')
ackermannWithCache(1, 1)
print('Starting with m = 2, n = 3:')
ackermannWithCache(2, 3)
print('Starting with m = 3, n = 3:')
ackermannWithCache(3, 3)
"""
print('Starting with m = 4, n = 3:')
ackermannWithCache(4, 3)