Skip to content

Instantly share code, notes, and snippets.

@sorenlouv
Last active February 27, 2016 19:33
Show Gist options
  • Save sorenlouv/a4aa77c03ec2b522dd40 to your computer and use it in GitHub Desktop.
Save sorenlouv/a4aa77c03ec2b522dd40 to your computer and use it in GitHub Desktop.
Pascal's Triangle Example
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