Created
December 5, 2013 19:30
-
-
Save dliberalesso/7811852 to your computer and use it in GitHub Desktop.
Script to generate a Pascal's Triangle.
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
def pascalsTriangle(n = 1) | |
return nil if n == nil || n < 1 | |
rows = [[1]] | |
2.upto(n) do |i| | |
row = [1] | |
1.upto(i-2) do |j| | |
row << rows.last[(j-1) % rows.last.size] + rows.last[j % rows.last.size] | |
end | |
rows << (row << 1) | |
end | |
rows.flatten | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment