Monday 7 April 2014

Complex recursion problem in C++ Ackermann function



The Ackermann function is a computable function which grows very, very quickly as its inputs grow. For example, while   and  are equal to  and  respectively, .
The Ackermann function can be defined as follows:
What are the last 3 digits of  

#include<iostream>
using namespace std;
int ack(long int m, long int n);
int main()
{
long int m, n,a ;
cout << "enter the m and n  : ";
cin >> m>>n;
a = ack(m, n);
cout << "Ackermann function (" << m << "," << n << ")" << "=" << a <<endl;
return 0;
}

int ack(long int m, long int n)
{

if (m==0) return (n+1);
if (m > 0 && n == 0) return (ack(m - 1, 1));
if (m > 0 && n > 0) return (ack(m - 1, ack(m, n - 1)));




}
output:
enter the m and n  : 3 9
Ackermann function (3,9)=4093

Process returned 0 (0x0)   execution time : 6.553 s

Press any key to continue.


No comments:

Post a Comment