Skip to content

Instantly share code, notes, and snippets.

@tonytonyjan
Created January 6, 2016 18:50
Show Gist options
  • Save tonytonyjan/9edfe5a8b101ba7c872a to your computer and use it in GitHub Desktop.
Save tonytonyjan/9edfe5a8b101ba7c872a to your computer and use it in GitHub Desktop.
Pascal's triangle
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