fork download
  1. //
  2. // stack, array, static node, recursive
  3. //
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. long long int recursive1(long long int n) {
  9. if (n == 0)
  10. return n;
  11.  
  12. //printf("%zd\n",n);
  13. return n + recursive1(n-1);
  14. }
  15.  
  16. //========================================================//
  17.  
  18. long long int recursive2(int n) {
  19. size_t current;
  20. size_t length;
  21.  
  22. struct node {
  23. long int n;
  24. int step;
  25. };
  26.  
  27. static struct node nodes[64000000];
  28. struct node node;
  29.  
  30. long long int rvalue;
  31.  
  32. length = 0;
  33. current = 0;
  34.  
  35. node.n = n;
  36. node.step = 0;
  37. //rvalue = 0;
  38.  
  39. nodes[length] = node;
  40. length++;
  41.  
  42. while (length > 0) {
  43. current = length-1;
  44. if (nodes[current].step == 0) {
  45. if (nodes[current].n == 0) {
  46. rvalue = 0;
  47. length--;
  48. continue;
  49. }
  50.  
  51. //printf("%ld\n", nodes[current].n);
  52.  
  53. nodes[length - 1].step++;
  54.  
  55. nodes[length].n = nodes[current].n - 1;
  56. nodes[length].step = nodes[current].step - 1;
  57. length++;
  58. } else if (nodes[current].step == 1) {
  59. rvalue += nodes[current].n;
  60. length--;
  61. }
  62. }
  63. return rvalue;
  64. }
  65.  
  66. //========================================================//
  67.  
  68. int main(void) {
  69.  
  70. //printf("recursive1(): %lld;\n\n", recursive1(50000000));
  71. printf("recursive2(): %lld;\n\n", recursive2(50000000));
  72.  
  73. return 0;
  74. }
  75.  
Success #stdin #stdout 0.61s 502272KB
stdin
Standard input is empty
stdout
recursive2(): 1250000025000000;