Skip to content

Commit 961ed91

Browse files
committed
pascalTriangle
1 parent a0341fe commit 961ed91

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

algorithms/math/pascalTriangle.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function pascalTriangle(lineNumber) {
2+
const currentLine = [1];
3+
4+
const currentLineSize = lineNumber + 1;
5+
6+
for (let numIndex = 1; numIndex < currentLineSize; numIndex += 1) {
7+
// See explanation of this formula in README.
8+
currentLine[numIndex] = currentLine[numIndex - 1] * (lineNumber - numIndex + 1) / numIndex;
9+
}
10+
11+
return currentLine;
12+
}
13+
14+
module.exports = pascalTriangle;

0 commit comments

Comments
 (0)