github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/execution/testdata-istanbul/tail-recursion.sol (about)

     1  pragma solidity ^0.8.4;
     2  
     3  contract MyContract {
     4  
     5      // naive recursion
     6      function sum(uint n) public returns(uint) {
     7          return n == 0 ? 0 :
     8            n + sum(n-1);
     9      }
    10  
    11      // loop
    12      function sumloop(uint n) pure public returns(uint) {
    13          uint256 total = 0;
    14          for(uint256 i=1; i<=n; i++) {
    15            total += i;
    16          }
    17          return total;
    18      }
    19  
    20      // tail-recursion
    21      function sumtailHelper(uint n, uint acc) private returns(uint) {
    22          return n == 0 ? acc :
    23            sumtailHelper(n-1, acc + n);
    24      }
    25      function sumtail(uint n) public returns(uint) {
    26          return sumtailHelper(n, 0);
    27      }
    28  }