-1

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
rigel
  • 485
  • 1
  • 6
  • 12

1 Answers1

7

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • thanks. I'd assumed that it automatically returned some sort of a value from the first branch. – rigel Jun 08 '14 at 16:56