The whole point of Ackerman's function is that it grows absurdly fast. So you have chosen the worst function to fight; it must be pathological. Even without you, you overflow the data type very quickly int.
There is one thing you can do to try to remember a function:
public static int Ackermann( int m, int n )
{
int result;
if( !smCache.TryGetValue( ( m, n ), out result ) )
{
result = AckermannImpl( m, n );
smCache.Add( ( m, n ), result );
}
return result;
}
private static int AckermannImpl( int m, int n )
{
if( m == 0 ) return n + 1;
else if( n == 0 ) return Ackermann( m - 1, 1 );
else return Ackermann( m - 1, Ackermann( m, n - 1 ) );
}
private static readonly Dictionary<(int, int), int> smCache = new Dictionary<(int, int), int>();
, Ackermann.
, Ack (5, 0), AckermannImpl 163847. Ack (6, 0) - ( ).
, , . : , - . ( , - ). :
private static int A( int m, int n )
{
while( true )
{
if( m == 0 ) return n + 1;
if( n == 0 )
{
m = m - 1;
n = 1;
continue;
}
n = A( m--, n - 1 );
}
}
memoization ( ), , memoization ( , ). , , BigInteger, .