fork(3) download
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5.  
  6. int ack_recursive(int m, int n) {
  7. if (m == 0) {
  8. return n + 1;
  9. }
  10.  
  11. if (n == 0) {
  12. return ack_recursive(m - 1, 1);
  13. }
  14.  
  15. return ack_recursive(m - 1, ack_recursive(m, n - 1));
  16. }
  17.  
  18. int ack_nonrecursive(int m, int n, int *status) {
  19. int value[n];
  20. size_t size = 0;
  21.  
  22. for (;;) {
  23. if (m == 0) {
  24. n++;
  25. if (size-- == 0) {
  26. *status = EXIT_SUCCESS;
  27. break;
  28. }
  29. assert(size < sizeof value / sizeof *value);
  30. m = value[size];
  31. continue;
  32. }
  33.  
  34. if (n == 0) {
  35. m--;
  36. n = 1;
  37. continue;
  38. }
  39.  
  40. size_t index = size++;
  41. assert(index < sizeof value / sizeof *value);
  42. value[index] = m - 1;
  43. n--;
  44. }
  45.  
  46. return n;
  47. }
  48.  
  49. int main(void) {
  50. for (int m = 0; m < 2; m++) {
  51. for (int n = 0; n < 2; n++) {
  52. int status;
  53. assert(ack_recursive(m, n) == ack_nonrecursive(m, n, &status) && status == EXIT_SUCCESS);
  54. }
  55. }
  56. }
Success #stdin #stdout 0s 9288KB
stdin
Standard input is empty
stdout
Standard output is empty