Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save magurofly/3780f2a33c81bbc0b2ae4cb637cef04b to your computer and use it in GitHub Desktop.
Save magurofly/3780f2a33c81bbc0b2ae4cb637cef04b to your computer and use it in GitHub Desktop.
Compute Ackermann function
def ack(m, n):
stack = [(m, 1)]
while len(stack):
m, c = stack.pop()
if m == 0:
n += c
elif m == 1:
n += 2 * c
elif m == 2:
n = 2**c * (n + 3) - 3
else:
if c >= 2:
stack.append((m, c - 1))
stack.append((m - 1, n + 1))
n = 1
return n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment