Just used the builtin function product
to make things a bit nicer.
module NCR where --Combinations nCr comb:: Integer -> Integer -> Integer comb n r = factorial n `div` (factorial r * factorial (n-r)) where factorial n = product [1 .. n]
- module NCR where
- --Combinations nCr
- comb:: Integer -> Integer -> Integer
- comb n r = factorial n `div` (factorial r * factorial (n-r))
- where
factorial n = foldr (*) 1 [2..n]- factorial n = product [1 .. n]
module NCR.TestSpec where import NCR import Test.Hspec import Test.QuickCheck main = hspec spec spec = do describe "test for nCr" $ do it "test nCr" $ do comb 3 2 `shouldBe` 3 comb 100 3 `shouldBe` 161700 comb 1000 10 `shouldBe` 263409560461970212832400
module NCR.Test where- module NCR.TestSpec where
- import NCR
- import Test.Hspec
- import Test.QuickCheck
main = hspec $ do- main = hspec spec
- spec = do
- describe "test for nCr" $ do
- it "test nCr" $ do
- comb 3 2 `shouldBe` 3
- comb 100 3 `shouldBe` 161700