I wanted to code a function (modified version of Ackermann's funciton) defined in A Tour Through Mathematical Logic by S. Wolf as follows:
A(0,n)=n+1 for every n
A(1,0)=2
A(2,0)=0
A(m+3,0)=1 for every m
A(m+1,n+1)=A(m,A(m+1,n)) for every m and n
To make a faster program, I used the fact that
A(1,n)=n+2
A(2,n)=2n
A(3,n)=2^n
Here's the code:
#include <iostream>
#include <cmath>
using namespace std;
long long A(long long m, long long n)
{
if(m==0)
return n+1;
else if(m==1)
return n+2;
else if(m==2)
return 2*n;
else if(m==3)
return pow(2,n);
else if(m>=4 && n==0)
return 1;
else
return A(m-1,A(m,n-1));
}
int main(void)
{
long long m,n;
cout << "m=";
cin >> m;
cout << "n=";
cin >> n;
cout << "A(m,n)=" << A(m,n) << endl;
return 0;
}
It seemed to work fine: by hand, A(5,3)=2^16 and that's the result that I get. However, the program outputs A(5,4)=4 and A(5,5)=2^16, whereas the correct result is really huge. I couldn't spot the mistake in my code. Could you tell me what's wrong please? Thank you in advance.