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.
This comment is hidden because it contains spoiler information about the solution
I had broken this same solution up into two parts. Great to know it could be done like this.
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.
not seem to be a good answer ,(although short)as it has to recall a more complicat function which takes more resource
This comment is hidden because it contains spoiler information about the solution
spoiler flag...
This comment is hidden because it contains spoiler information about the solution
I have forgotten regExp^)
This has to me a lot...not on blues but yellows should be giving 8...been getting 2.
no curly brackets, no print in ruby it's puts(don't need it for this problem)
https://ruby-doc.org/core-2.2.2/Integer.html#method-i-times