The below function is exceeding the maximum call stack when running on node.js v8.5 on OS X 10.13 (High Sierra)
"use strict";
function sum_multiples(i, max, sum){
if(i >= max) return sum;
return ((i%3 === 0) || (i%5 === 0)) ? sum_multiples(i+1, max, sum+i) : sum_multiples(i+1, max, sum);
}
console.log(sum_multiples(1, 1000, 0));
Should this be structured differently to avoid exceeding the call stack or is the problem the engine being used?
Using a while
or for
loop doesn't cause this problem and the machine that this is running on can compute the solution for this for 1,000,000,000,000 using those methods.