Ackerman function - Python

Asked

Viewed 1,697 times

1

Well, I’m still learning to code in python and I came across exercise that I can’t 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 like "," and "A" itself confuse me. If anyone knows how to solve I would be grateful.

enter image description here

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

  • A is the name of the function. If A in its definition (body), this means that it will be called recursively in some situations. And the comma simply separates the arguments - this function A takes two numbers as argument (here called m and n) and returns a single number, given the definition. Is it clearer? Do you understand what a recursive function is? (whether in mathematics, or in programming)

  • Yes, a recursive function is one that calls itself in the "body" of the function, right ? Yes clarified some doubts , then, if hypothetically I had a group (1,3) the A would be replaced by the same and multiply the sentence between "( )" following ?

  • I don’t understand what you mean. If you have a pair (1,3) and wants to apply the function A, i and.. A(1,3), you would be associating to the parameter m the value 1 and the parameter n the value 3. By definition, m = 0 is false and m > 0 and n = 0 is false, so the function value at those points would be A(m - 1, A(m, n - 1)). Replacing the parameters with their values, we have A(1 - 1, A(1, 3 - 1)) which is equal to A(0, A(1, 2)). Then you’d have to calculate A(1, 2) - by the same process - and use its value to calculate A(0, ...) - also by the same process.

  • P.S. Please do not use the answer space to comment on the question. You can always comment on your own posts ("comment" link under the question/answers) or - if you have additional information to improve the question - you can also edit the question (link "edit") with this information. I asked a moderator to convert his reply to comment, ok?

  • Ah yes now I understood :) thank you very much for the help and sorry for the answer, this is my first post and I do not know very well how it works. Thank you very much

  • 1

    Oi, Otávio, if you need help in the use of the site, has the support area [meta]. The site has some very specific rules that the [help] clarifies very well, just spend a little time there and then leave for the hug.

Show 1 more comment

2 answers

1

Here is 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 wheel 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))

1

This is a recursive function.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.