Created
January 6, 2016 18:50
-
-
Save tonytonyjan/9edfe5a8b101ba7c872a to your computer and use it in GitHub Desktop.
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
| exit if (level = ARGV[0].to_i) <= 0 | |
| def combination n, r | |
| return 1 if r == 0 | |
| n.downto(n-r+1).inject(:*) / r.downto(1).inject(:*) | |
| end | |
| max_length = combination(level-1, (level-1)/2).to_s.length | |
| ary = [1] | |
| 0.upto(level-1).each do |i| | |
| i.downto(1).each do |j| | |
| ary[j] = 0 if ary[j].nil? | |
| ary[j] += ary[j-1] | |
| end | |
| print ' ' * max_length * (level - i - 1) | |
| puts ary.map{|n| "%#{max_length}d" % n }.join(' ' * max_length) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment