Last active
February 27, 2016 19:33
-
-
Save sorenlouv/a4aa77c03ec2b522dd40 to your computer and use it in GitHub Desktop.
Pascal's Triangle Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pascalTriangle(10); | |
function pascalTriangle(levels) { | |
return _.reduce(_.range(levels), function(triangle, i) { | |
var row = _.reduce(_.range(i + 1), function(row, j) { | |
var isFirst = j === 0; | |
var isLast = j === i; | |
if (isFirst || isLast) { | |
row.push(1); | |
} else { | |
var sum = triangle[i - 1][j - 1] + triangle[i - 1][j]; | |
row.push(sum); | |
} | |
return row; | |
}, []) | |
triangle.push(row); | |
return triangle; | |
}, []) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment