Ackermann Function

Hi

I need to put the Ackermann Function in NOT recursive code.

Do you know any idea how to make it iterally ??

The function is defined like that

A(m ; n) =

= n+1 if m=0
= A(m-1 ; 1 ) if (m>0 and n=0)
= A(m-1 ; A(m ; n-1)) if else

Thanks for all the suggetions
Stebel, Poland.

Comments

  • : Hi
    :
    : I need to put the Ackermann Function in NOT recursive code.
    :
    : Do you know any idea how to make it iterally ??
    :
    : The function is defined like that
    :
    : A(m ; n) =
    :
    : = n+1 if m=0
    : = A(m-1 ; 1 ) if (m>0 and n=0)
    : = A(m-1 ; A(m ; n-1)) if else
    :
    : Thanks for all the suggetions
    : Stebel, Poland.
    :
    :
    Here is a code, which should result in the pseudocode you gave. I don't know if its correct, but it uses an iterative method of getting the result.
    [code]
    function A(m, n: integer): integer;
    begin
    if m = 0 then
    Result := n + 1
    else if (m > 0) and (n = 0) then
    Result := A(m-1, 1)
    else
    Result := A(m-1, A(m, n-1));
    end;
    [/code]
  • : : Hi
    : :
    : : I need to put the Ackermann Function in NOT recursive code.
    : :
    : : Do you know any idea how to make it iterally ??
    : :
    : : The function is defined like that
    : :
    : : A(m ; n) =
    : :
    : : = n+1 if m=0
    : : = A(m-1 ; 1 ) if (m>0 and n=0)
    : : = A(m-1 ; A(m ; n-1)) if else
    : :
    : : Thanks for all the suggetions
    : : Stebel, Poland.
    : :
    : :
    : Here is a code, which should result in the pseudocode you gave. I don't know if its correct, but it uses an iterative method of getting the result.
    : [code]
    : function A(m, n: integer): integer;
    : begin
    : if m = 0 then
    : Result := n + 1
    : else if (m > 0) and (n = 0) then
    : Result := A(m-1, 1)
    : else
    : Result := A(m-1, A(m, n-1));
    : end;
    : [/code]
    :

    Unfortunately, it's still calling itself here, so it still is recursive. Why not try using [b]While NOT ()[/b] functions with maybe some arrays? It's going to take some time, but there's sure to be a way. My instructor said that you couldn't do the Towers of Hanoi without recursion (so obviously, I took the challenge ;)
    Keep on it. It will work itself out.

    Phat Nat


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories