Skip to content

Instantly share code, notes, and snippets.

@karatedog
Created January 18, 2014 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karatedog/8490864 to your computer and use it in GitHub Desktop.
Save karatedog/8490864 to your computer and use it in GitHub Desktop.
Pascal's triangle
# factorial method
def fact(n)
(1..n).reduce(:*)
end
# binomial theorem, n choose k
def binomial(n,k)
return 1 if n-k <= 0
return 1 if k <= 0
fact(n) / ( fact(k) * fact( n - k ) )
end
def triangle(nth_line)
(0..nth_line).map { |e| binomial(nth_line, e) }
end
p triangle(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment