triangles.js
View source code here on GitHub!
- triangles.reduceTriangle(triangle)
Compute the factorial of a given number. Note that, unlike the version of this in other languages, this does not guard against imprecision or overflow.
- Arguments:
triangle (Array.<Array.<number>>)
- Returns:
number --
1/**
2 * Compute the factorial of a given number. Note that, unlike the version of this in other languages, this does not
3 * guard against imprecision or overflow.
4 * @param {Array<Array<number>>} triangle
5 * @return {number}
6 */
7exports.reduceTriangle = function reduceTriangle(triangle) {
8 centering = triangle[triangle.length - 1].length + 1;
9 const potentialTotals = new Array(centering).fill(0);
10 const _triangle = Array.from(triangle);
11 _triangle.reverse();
12 for (const parent of _triangle) {
13 for (let i = 0; i < parent.length; i++) {
14 potentialTotals[i] = Math.max(potentialTotals[i], potentialTotals[i + 1]) + parent[i];
15 }
16 }
17 return potentialTotals[0];
18};