Tell us what’s happening:
So my code below works for the first 3 test-cases but seems to time-out for the large n-values. How might I go about improving the performance of my code so it doesn’t time-out?
Your code so far
function divisibleTriangleNumber(n) {
function triCalc(x){
return (x*(x+1))/2
}
function calcDivs(num){
let divCount = 0
for (let j = 1; j <= num; j++){
if (num%j===0){
divCount++
}
}
return divCount
}
let i = 1
let triVal, totalDivs;
while (totalDivs <= n){
triVal = triCalc(i)
totalDivs = calcDivs(triVal)
i++
}
return triVal
}
divisibleTriangleNumber(500);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-12-highly-divisible-triangular-number/