c++ackermann

Ackermann function don't work properly in C++


In my home work of Ackermann function I have solved the problem as following

int main()
{
    int y = ack(4,1);
    cout<<"ans is :::: "<< y;

    getch();
    return 0;
}

int ack(int m, int n)
{
    if(m == 0)
    {
        return n+1; 
    }
    else if(m > 0 && n == 0)
    {
        return ack(m-1,1);  
    }
    else if(m > 0 && n>0)
    {
        int x = ack(m,n-1);
        return ack(m-1,x);
    }
    else 
    {
        cout<< "did not worked properly";
    }   
}

This function works great with low values upto m=3 and n = 10 But when I give m = 4/above or n = 15/above this don't work. I get no out put. Program just exit without any warning or error or result.

Please some body tell me the reason why this is happening and how can I solve this problem.


Solution

  • The number (4, 15) is such a big number that is impossible to calculate and represent. Look at the table of values. For example (4, 2) is orders of magnitude bigger than number of particles in the observable universe!

    I had a similar homework. The whole point is to show you how insanely something can grow. Humans have problem to grokk exponential growth which is pale in comparison to Ackermann function.

    Thinking about big numbers can lead to interesting conclusions. Imagine, you are walking along the road which is 2^2^65536 - 3 meters long (that's ackermann(4, 3)). Assuming that average human body is roughly equal to 1m^3 it has got 10^10^70 quantum states. Going down the road you will meet your doppelgangers - exact doppelgangers on quantum level! So they will have execly same thoughts, same scars, itchy elbow in the same spot. They even going to digest same food. You will meet billions of billions of billions doppelgangers. For me, it's really mind-blowing.