You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18 lines
453 B
18 lines
453 B
module Binomial where
|
|
|
|
import Data.MemoTrie
|
|
|
|
-- binomial coefficients
|
|
binomial :: Int -> Int -> Integer
|
|
binomial = memo2 b where
|
|
b n k = if k > n
|
|
then 0
|
|
else if n <= 5
|
|
then table !! n !! k
|
|
else binomial (n-1) k + binomial (n-1) (k-1)
|
|
table = [ [1]
|
|
, [1, 1]
|
|
, [1, 2, 1]
|
|
, [1, 3, 3, 1]
|
|
, [1, 4, 6, 4, 1]
|
|
, [1, 5, 10, 10, 5, 1] ]
|
|
|