0

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)?

  • 3
    Are you aware that the value for `ack(5, 5)` has so many digits that it will not fit into the RAM of any computer yet invented? What exactly are you trying to accomplish? You can find a representation of `ack(5, 5)` [here](https://en.wikipedia.org/wiki/Ackermann_function#Table_of_values) though this does not show the digits. – Rory Daulton Aug 14 '18 at 09:39
  • I tried computing the value of ack(4,1) in C, it returned me a value, but why does it seem not possible in Python though? – Hema Prasath Aug 14 '18 at 09:44
  • 2
    Did it return a number, or did it return the correct number? –  Aug 14 '18 at 09:47
  • Can you not re-write it as non-recursive? – Jon Clements Aug 14 '18 at 09:48
  • @P i I tried defining the Ackermann function as close as to the definition of the Ackermann function: https://wikimedia.org/api/rest_v1/media/math/render/svg/1a15ea2fcf1977e497bccdf1916ae23edc412fff – Hema Prasath Aug 14 '18 at 10:04
  • Memoization might help you for a while – L3viathan Aug 14 '18 at 10:35

0 Answers0