How do I allow python to recursively compute beyond the set recursive limit?

316 Views Asked by At

Just out of curiosity, I decided to take an example of a recursive function. So, I have this recursive function (The Ackermann function):

def ack(a, b):
    if a == 0:
        return b + 1
    elif b == 0:
        return ack(a - 1, 1)
    else:
        return ack(a - 1, ack(a, b - 1))


for i in range(5):
    for j in range(5):
        print("ack(", i, ",", j, ")", "=", ack(i, j))

As you can see, I tried computing for Ackermann values till ack(5,5).

But I get the following error, after printing till ack(4,0):

RecursionError: maximum recursion depth exceeded in comparison

So I tried increasing the recursion limit to a particularly high value as:

import sys
sys.setrecursionlimit(30000)

But now I get the values till ack(4,0) and the Kernel dies.

ack( 0 , 0 ) = 1
ack( 0 , 1 ) = 2
ack( 0 , 2 ) = 3
ack( 0 , 3 ) = 4
...
ack( 4 , 0 ) = 13

Kernel died, restarting

How do I compute the values for higher values than ack(4,0) in python? Edit: Just realized the value ack(4,2) is practically incomputable in today's world. So, how do I compute ack(4,1)?

0

There are 0 best solutions below