Last updated 4 years ago
Was this helpful?
end number of each line of number pyramid is *previous-last-number + line number
which means the 5th end number will *previous-last-number(4th) + 4
= (4th last number is 10) + 5 = 15
const sumTriangularNumbers = (n, count = 1, previousEndNum = 0, sum = 0) => { return count <= n ? sumTriangularNumbers( n, count + 1, previousEndNum + count, sum + previousEndNum + count ) : sum; };
time complexity is O(n)