Recursion: Ackerman's Function
Write a program for Ackerman's function using Recursion
Ack(3,0) = ack(2,1)
= ack(1, ack(2,0))
= ack(1, ack(1,1))
= ack(1, ack(0, ack(1,0)))
= ack(1, ack(0, ack(0, 1)))
= ack(1, ack(0, 2))
= ack(1, 3)
= ack(0, ack(1, 2))
= ack(0, ack(0, ack(1, 1)))
= ack(0, ack(0, ack(0, ack(1, 0))))
= ack(0, ack(0, ack(0, ack(0, 1))))
= ack(0, ack(0, ack(0, 2)))
= ack(0, ack(0, 3))
= ack(0, 4)
= 5
#include<stdio.h>
double acker(int
m, int n);
void main()
{
int
m, n;
double
a;
printf("\nEnter
the value of m and n:");
scanf("%d
%d", &m, &n);
if(m<0
|| n<0)
printf("\nEnter
nonnegatives only");
a
= acker(m, n);
printf("\n
A(%d , %d) = %lf", m, n, a);
}
double acker(int
m, int n)
{
if(
m == 0 )
return
n+1;
else
if(n == 0)
return acker((m-1),
1);
else
return acker(
(m-1), acker(m, n-1) );
}
Output:
Enter the value of
m and n: 3 0
A(3 , 0) = 5.000000