Last active
February 28, 2016 12:44
-
-
Save adekbadek/57096e078d65e3bfd73e to your computer and use it in GitHub Desktop.
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
// 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