Created
January 21, 2015 21:38
-
-
Save joegiralt/601d95496c49b4988fa2 to your computer and use it in GitHub Desktop.
n choose k Ruby
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
class Integer | |
def choose(k) | |
return 0 if (k > self) | |
n = self | |
r = 1 | |
1.upto(k) do |d| | |
r *= n | |
r /= d | |
n -= 1 | |
end | |
return r | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment