Created
November 9, 2019 21:05
-
-
Save mbeltagy/34d3b3c29dc62a9802aa9d4373518fb2 to your computer and use it in GitHub Desktop.
Pascal Triangle calculation
This file contains 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
"Pascal triangle" | |
function pascal(n) | |
(n<=0) && error("Pascal trinalge can not have zero or negative rows") | |
r=Vector{Int}(undef,n) | |
pr=Vector{Int}(undef,n) | |
pr[1]=r[1]=1 | |
println(@view pr[1]) | |
for i=2:n | |
r[1]=r[i]=1 | |
for j=2:i-1 | |
r[j]=pr[j-1]+pr[j] | |
end | |
println(join(view(r,1:i), " ")) | |
r,pr=pr,r | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment