Created
April 15, 2015 16:37
-
-
Save JanneSalokoski/09c8cfe5390a6f38a04f to your computer and use it in GitHub Desktop.
A python implementation of the ackermann function from here: http://www.eprg.org/computerphile/ackermann.c
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
#!/usr/bin/python3 | |
import sys | |
sys.setrecursionlimit(100000) #Python seems to be a sissy language by default. | |
def ack(m, n): | |
if m == 0: | |
ans = n + 1 | |
elif n == 0: | |
ans = ack(m - 1, 1) | |
else: | |
ans = ack(m - 1, ack(m, n - 1)) | |
return ans | |
def main (): | |
for i in range(0, 6): | |
for j in range(0, 6): | |
ackerman = "Ackerman ({},{}) is: {}".format(i, j, ack(i, j)) | |
print(ackerman) | |
main() |
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
$ ./ack.py | |
Ackerman (0,0) is: 1 | |
Ackerman (0,1) is: 2 | |
Ackerman (0,2) is: 3 | |
Ackerman (0,3) is: 4 | |
Ackerman (0,4) is: 5 | |
Ackerman (0,5) is: 6 | |
Ackerman (1,0) is: 2 | |
Ackerman (1,1) is: 3 | |
Ackerman (1,2) is: 4 | |
Ackerman (1,3) is: 5 | |
Ackerman (1,4) is: 6 | |
Ackerman (1,5) is: 7 | |
Ackerman (2,0) is: 3 | |
Ackerman (2,1) is: 5 | |
Ackerman (2,2) is: 7 | |
Ackerman (2,3) is: 9 | |
Ackerman (2,4) is: 11 | |
Ackerman (2,5) is: 13 | |
Ackerman (3,0) is: 5 | |
Ackerman (3,1) is: 13 | |
Ackerman (3,2) is: 29 | |
Ackerman (3,3) is: 61 | |
Ackerman (3,4) is: 125 | |
Ackerman (3,5) is: 253 | |
Ackerman (4,0) is: 13 | |
Segmentation fault (core dumped) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apparently python does everything in it's power to make me not use recursion. At least like this.