I am trying to run the Ackermann function1 and I am having an issue. I am trying to run the program through my Command Prompt, on a Windows 10 machine, and a few seconds after reaching the value pair (4,0) the program stops. I am assuming because it has run out of available resources, but I am not sure at all. Is there a way to fix this issue? TIA.
I am using the MinGw G++ compiler [8.1.0].
Here is the code in question as well.
#include <iostream>
#include <ctime>
int ack(int m, int n)
{
int ans;
if (m == 0)
ans = n + 1;
else if (n == 0)
ans = ack(m - 1, 1);
else
ans = ack(m - 1, ack(m, n - 1));
return ans;
}
void runAck(time_t start)
{
int answer;
double seconds;
time_t now_t;
for(int i = 0; i <= 6; i++)
{
for(int j = 0; j<= 6; j++)
{
answer = ack(i, j);
time(&now_t);
seconds = difftime(now_t, start);
printf("Ackermann(%i, %i) is %i. Found in %.3f seconds.\n", i, j, answer, seconds);
}
}
}
int main()
{
std::cout << "Testing of the Ackermann recursion." << std::endl;
time_t start = time(NULL);
runAck(start);
return 0;
}
I initially thought it was because I was using VSCode to access the command prompt but when I use it directly there is no change. While writing this question, I just thought of trying out a different OS or Ubuntu to see if there is any difference.
My goal is just to get to (4,1) to see how much faster it is now to calculate.
1 The Ackermann Function is a recursion function that was developed as computer theory to create a function that can only be calculated recursively.
Edit 1 @MikeCAT helped me realize what I was truly asking and answered my question.