Created
May 6, 2024 04:35
-
-
Save magurofly/3780f2a33c81bbc0b2ae4cb637cef04b to your computer and use it in GitHub Desktop.
Compute Ackermann function
This file contains 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
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