Created
June 20, 2019 07:00
-
-
Save carlosgmartin/7c0381e4ac5ff2256962ad3791fef458 to your computer and use it in GitHub Desktop.
Stack-based implementation of the Ackermann function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.sciencedirect.com/science/article/pii/0304397588900461 | |
def stackermann(i, n): | |
stack = [] | |
stack.append(i) | |
stack.append(n) | |
while len(stack) > 1: | |
n = stack.pop() | |
i = stack.pop() | |
if i == 0: | |
stack.append(n + 1) | |
elif n == 0: | |
stack.append(i - 1) | |
stack.append(1) | |
else: | |
stack.append(i - 1) | |
stack.append(i) | |
stack.append(n - 1) | |
return stack.pop() | |
n = 8 | |
assert stackermann(0, n) == n + 1 | |
assert stackermann(1, n) == n + 2 | |
assert stackermann(2, n) == 2*n + 3 | |
assert stackermann(3, n) == 2**(n+3) - 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment