Ackerman function - Python

1

Well, I'm still learning to program in python and I came across exercise that I do not know how to solve.

I need to write a function that solves Ackermann's famous function. I think I will not have problems with the syntax and logic behind the function itself, the problem and the calculation. Symbols such as "," and "A" themselves confuse me. If anyone knows how to solve it would be grateful.

PS: I'm interested in how to solve the function, the code in python would be on my own.

    
asked by anonymous 14.10.2015 / 00:23

2 answers

1

This is a recursive function.

You should create a ackerman function that receives two parameters: m and n. Inside it, it will place a sequence of type if ... else if ... else testing all three cases. In recursive cases, you must return the value of ackerman with the appropriate parameters. On the last else (which indicates that some of the parameters are negative), throw an invalid parameter exception.

    
05.11.2015 / 04:25
1

Here's your function! This function is the origin of the recursive function. In it you have to have two parameters that follow those rules that you passed in the mathematical formula, after that, you take the parameters and the wheel in order to reach this function:

#Função de Ackerman
calls = 0

def ackerman(m, n):
    global calls
    calls += 1
    if m == 0:
        return n + 1
    elif n == 0:
        return ackerman(m - 1, 1)
    else:
        return ackerman(m - 1, ackerman(m, n - 1))

print(ackerman(m,n))
    
05.01.2016 / 05:35