Ad
  • Custom User Avatar

    How does the solution have to call a more complicated function? sum() is not more complicated than your solution--it eventually resolves to a while loop that simply loops over the iterable, accumulating the sum along the way, and then returns it. Your solution, on the other hand, recursively calls itself n-1 times, setting up a new stack frame each time, and consuming n frames on the stack, which is resource-intensive in both time and space. Your solution isn't even tail recursive, so tail call optimization cannot be used to reduce these costs.