Why is this failing? I have written Ackermann's function in C and used longs to make sure that no number will be too small. Yet, when I go above (including) 4 for m and n, it gives me a segmentation fault: 11
. Does anyone know why?
#include <stdio.h>
int ackermann(long m, long n) {
if (m == 0)
return n + 1;
else if (m > 0 && n == 0)
return ackermann(m - 1, 1);
else if (m > 0 && n > 0)
return ackermann(m - 1, ackermann(m, n - 1));
}
int main() {
long result = ackermann(4, 4);
printf("%lu", result);
}