Created
October 21, 2011 21:44
-
-
Save selman/1305060 to your computer and use it in GitHub Desktop.
This file contains 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
# http://en.wikipedia.org/wiki/Combination#Example_of_counting_combinations | |
module Combination | |
def self.size n, k | |
size_r(n, k).to_i | |
end | |
def self.size_r n, k | |
return 1 if k.zero? | |
Rational(n - (k - 1), k) * size_r(n, k - 1) | |
end | |
end | |
puts Combination.size(52, 5) |
This file contains 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
-- http://en.wikipedia.org/wiki/Combination#Example_of_counting_combinations | |
module Main where | |
import Data.Ratio ((%), numerator) | |
combinationSize :: (Integral a) => a -> a -> a | |
combinationSize n k = numerator $ cSize n k | |
where cSize _ 0 = 1 | |
cSize n' k' = (n' - (k' - 1)) % k' * cSize n' (k' - 1) | |
main :: IO () | |
main = print $ combinationSize 52 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment