Skip to content

Instantly share code, notes, and snippets.

@selman
Created October 21, 2011 21:44
Show Gist options
  • Save selman/1305060 to your computer and use it in GitHub Desktop.
Save selman/1305060 to your computer and use it in GitHub Desktop.
# 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)
-- 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