recursion-Arkermann function- code run but have wrong answer

2 views (last 30 days)
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

Accepted Answer

Thorsten
Thorsten on 8 Sep 2016
Edited: Thorsten on 8 Sep 2016
You use a wrong definition of the Ackermann function. Must read:
A(m 1, 1) if m > 0 and n = 0(!!!);

More Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!