Question

The Ackermann's Function had been tried to implement through the following code

def A(m, n):
    if m == 0:
        return n + 1
    elif m > 0 and n == 1:
        A(m - 1, 1)
    elif m > 0 and n > 0:
        A(m - 1, A(m, n - 1))

print A(4, 5)
Was it helpful?

Solution

Your function doesn't return anything for 2 of the 3 branches of the if statements; only if m == 0 do you explicitly return a value.

You need to return the results of recursive calls too:

def A(m, n):
    if m == 0:
        return n + 1
    elif m > 0 and n == 1:
        return A(m - 1, 1)
    elif m > 0 and n > 0:
        return A(m - 1, A(m, n - 1))

Without an explicit return, the function ends with the default return value of None.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top