recursion-Arkermann function- code run but have wrong answer

2 ビュー (過去 30 日間)
Tho Gonzalez
Tho Gonzalez 2016 年 9 月 8 日
編集済み: Tho Gonzalez 2016 年 9 月 8 日
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(!!!);
  1 件のコメント
Tho Gonzalez
Tho Gonzalez 2016 年 9 月 8 日
編集済み: Tho Gonzalez 2016 年 9 月 8 日
Oh, the question says m>0 and n==1 ???

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by