Skip to content

Instantly share code, notes, and snippets.

@adekbadek
Last active February 28, 2016 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adekbadek/57096e078d65e3bfd73e to your computer and use it in GitHub Desktop.
Save adekbadek/57096e078d65e3bfd73e to your computer and use it in GitHub Desktop.
// Print n lines of Pascal's triangle
// just a factorial function
function fa(num) {
if (num === 0){ return 1; }
else{ return num * fa( num - 1 ); }
}
// Binomial coefficient
function newt(n, k){
return parseInt(
fa(n) / (fa(k) * fa(n-k) )
);
}
// print a single line of Pascal's triangle
var make_line = function(n){
// n = no. of the line to be returned
var line = [];
for(var i=0; i<=n; i++){
line.push(newt(n, i));
}
return line;
};
// print n lines of Pascal's triangle
var pascal = function(n){
for(var i=0; i<=n; i++){
console.log( make_line(i) );
}
};
// for line no. 10
console.log(make_line(10));
// for 5 lines:
pascal(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment