module NCR where --Combinations nCr comb:: Integer -> Integer -> Integer comb n r | n/=r = (factorial n) `div` (factorial r * factorial (n-r) ) | n==r = (factorial n) `div` (factorial r) factorial n= foldl (*) 1 [1..n]
- module NCR where
- --Combinations nCr
- comb:: Integer -> Integer -> Integer
comb n r | n/=r = (foldl (*) 1 [1..n]) `div` ((foldl (*) 1 [1..r]) * (foldl (*) 1 [1..(n-r)]))| n==r = (foldl (*) 1 [1..n]) `div` ((foldl (*) 1 [1..r]))- comb n r | n/=r = (factorial n) `div` (factorial r * factorial (n-r) )
- | n==r = (factorial n) `div` (factorial r)
- factorial n= foldl (*) 1 [1..n]
Calculated the combinations https://en.wikipedia.org/wiki/Combination
module NCR where
--Combinations nCr
comb:: Integer -> Integer -> Integer
comb n r | n/=r = (foldl (*) 1 [1..n]) `div` ((foldl (*) 1 [1..r]) * (foldl (*) 1 [1..(n-r)]))
| n==r = (foldl (*) 1 [1..n]) `div` ((foldl (*) 1 [1..r]))
module NCR.Test where
import NCR
import Test.Hspec
import Test.QuickCheck
main = hspec $ 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