The Ackermann function is the simplest example of a well-defined total function which is computable but not primitive recursive. The Ackermann function is one of the fastest growing functions, much faster than polynomials or exponentials.
Its definition is:
1. If x = 0 then A(x, y) = y + 1
2. If y = 0 then A(x, y) = A(x-1, 1)
3. Otherwise, A(x, y) = A(x-1, A(x, y-1))
You have to write a program to read two integers x and y, and print the value of A(x, y).
Warning: Do not try values larger than x=5 and y=5.
Its definition is:
1. If x = 0 then A(x, y) = y + 1
2. If y = 0 then A(x, y) = A(x-1, 1)
3. Otherwise, A(x, y) = A(x-1, A(x, y-1))
You have to write a program to read two integers x and y, and print the value of A(x, y).
Warning: Do not try values larger than x=5 and y=5.
Sample Input and Output:
Input:
00
Output:
1
Input:
21
Output:
5
Input:
32
Output:
29
00
Output:
1
Input:
21
Output:
5
Input:
32
Output:
29
Solution:
#include <stdio.h>
int arkman(int x, int y)
{
if(x==0)
return(y+1);
else if(y==0)
return(arkman(x-1,1));
else
return(arkman(x-1, arkman(x,y-1)));
}
void main()
{
int i,j;
scanf("%d %d",&i,&j);
printf("%d\n",arkman(i,j));
}
No comments:
Post a Comment