-
Notifications
You must be signed in to change notification settings - Fork 1
/
ackermann.py
45 lines (40 loc) · 1009 Bytes
/
ackermann.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
40
41
42
43
44
45
#! /usr/bin/env python
#-*- coding: utf-8 -*-
import sys
import resource
# Increases Python's recursion limit and the size of the stack.
resource.setrlimit(resource.RLIMIT_STACK, (2**29,-1))
sys.setrecursionlimit(10**6)
def naive_ackermann(m, n):
"""
Directly from definition
"""
if m == 0:
return n + 1
elif n == 0:
return naive_ackermann(m - 1, 1)
else:
return naive_ackermann(m - 1, naive_ackermann(m, n - 1))
def formula_ackermann(m, n):
"""
Precomputed for small m
"""
while m >= 4:
if n == 0:
n = 1
else:
n = formula_ackermann(m, n - 1)
m -= 1
if m == 3:
return (1 << n + 3) - 3
elif m == 2:
return (n << 1) + 3
elif m == 1:
return n + 2
else:
return n + 1
if __name__ == "__main__":
# Point of entry in execution mode
m, n = int(sys.argv[1]), int(sys.argv[2])
print(naive_ackermann(m, n))
#print(formula_ackermann(m, n))