View Single Post
Old Oct 19th, 2006, 2:07 AM   #6
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
/*
There's a well known function called the Ackermann function. This function usually appears in computer science courses and texts when the topic of recursion is discussed. The function is defined as:

Use the following function prototype to implement the Ackermann function as a recursive function:

long ack(long,long);

Some values for the Ackermann function are as follows:

    ackerman(0,0)= 0
    ackerman(1,0)= 0
    ackerman(1,1)= 2
    ackerman(1,2)= 4
    ackerman(1,3)= 8
    ackerman(2,1)= 2
    ackerman(2,2)= 4
    ackerman(2,3)= 16
    ackerman(3,1)= 2
    ackerman(3,2)= 4 

The program interaction should look as follows (user inputs are in bold):

Welcome to the Ackermann program.

Enter values for x and y(-1 -1 to exit): 0 0

value is: 0

Enter values for x and y(-1 -1 to exit): 2 3

value is: 16

Enter values for x and y(-1 -1 to exit): -1 -1

Thank you for using the Ackermann program and have a wonderful day.
*/


#include <iostream>
using namespace std;

long ack(long, long);

int main()
{
	long m, n;

	while(1)
	{
		cout<<"Welcome to the Ackermann program.  \n\n"<<endl;
		cout<<"Enter values for x and y(-1 -1 to exit):  \t"<<endl;
		cin>>m>>n;

		if (m== -1 && n== -1)
		{
			cout<<"Thank you for using the Ackermann program and have a wonderful day."<<endl;
			break;
		}

		else
			cout<<ack(m, n)<<"\n\n"<<endl;

	}

	return 0;
}

long ack(long m,long n)
{
	if (m == 0)
		return (2*n);

	if (m >= 1 && n ==0)
		return 0;

	if (m >= 1 && n ==1)
		return 2;

	if (m >= 1 && n >= 2)
		return ( ack( (m - 1), (ack(m, (n-1) ) ) ) );
}
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote