recursion-Arkermann function- code run but have wrong answer

2 次查看(过去 30 天)
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

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by