Created
January 18, 2014 13:52
-
-
Save karatedog/8490864 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
# 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