In: Computer Science
C++
Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a function A(m, n) that solves Ackermann’s function. Use the following logic in your function:
If m = 0 then return n + 1
If n = 0 then return A(m−1, 1)
Otherwise, return A(m–1, A(m, n–1))
Test your function in a driver program that displays the following values:
A(0, 0) A(0, 1) A(1, 1) A(1, 2) A(1, 3) A(2, 2) A(3, 2)
#include<iostream>
using namespace std;
int ackermann(int m, int n){
if(m == 0){
return n+1;
}
else if(m>0 && n==0){
return ackermann(m-1,1);
}
else{
return ackermann(m-1,ackermann(m,n-1));
}
}
int main(){
cout<<"ackermann(0,0) = "<<ackermann(0,0)<<endl;
cout<<"ackermann(0,1) = "<<ackermann(0,1)<<endl;
cout<<"ackermann(1,1) = "<<ackermann(1,1)<<endl;
cout<<"ackermann(1,2) = "<<ackermann(1,2)<<endl;
cout<<"ackermann(1,3) = "<<ackermann(1,3)<<endl;
cout<<"ackermann(2,2) = "<<ackermann(2,2)<<endl;
cout<<"ackermann(3,2) = "<<ackermann(3,2)<<endl;
return 0;
}

