Ad
Code
Diff
  • 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]))