MATLAB: Recursion-Arkermann function- code run but have wrong answer

ackermann

The Ackermann function, A, is a quickly growing function that is defined by the recursive relationship: A(m, n) = n +1 if m = 0; A(m − 1, 1) if m > 0 and n = 1; A(m − 1, A(m, n − 1)) if m > 0 and n > 0 Write a function with header [A] = myAckermann(m,n), where A is the Ackermann function computed for m and n. // My code run but it give wrong result , can you help me correct ?
if true
% code

end
function [A]= myAckermann(m,n)
%--------------------------------------------------------------------------
% recursion
%A(m,n) = n+1 if m=0
%A(m,n) = A(m-1,1) if m>0 and n=1
%A(m-1,A(m,n-1)) if m>0 and n>0
%Tho Huynh
%9/8/2016
%-------------------------------------------------------------------------
if m==0
A = n+1;
elseif m>0 && n==1
A= myAckermann(m-1,1);
else m>0 && n>0
A= myAckermann(m-1,myAckermann(m,n-1));
end
end
if true
% code
end

Best Answer

  • You use a wrong definition of the Ackermann function. Must read:
    A(m 1, 1) if m > 0 and n = 0(!!!);