Skip to content

Instantly share code, notes, and snippets.

@samrat
Created December 22, 2014 15:53
Show Gist options
  • Save samrat/d5fc0bcd08fb72f3f713 to your computer and use it in GitHub Desktop.
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.
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