Created
December 22, 2014 15:53
-
-
Save samrat/d5fc0bcd08fb72f3f713 to your computer and use it in GitHub Desktop.
Create a lower triangular n × n matrix filled with the first n rows of Pascal’s triangle.
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
module Pascal | |
export pascal | |
function factorial(n) | |
if n==0; return(1); end | |
result = n * factorial(n-1) | |
return(result) | |
end | |
function choose(n, r) | |
result = factorial(n) / (factorial(r) * factorial(n-r)) | |
return(result) | |
end | |
function pascal_element(i, j) | |
if i <= j; return(choose(j,i)); end | |
return(0.0) | |
end | |
function pascal(n) | |
return([pascal_element(i,j) for j in [0:(n-1)], i in [0:(n-1)]]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment