Combinatorics in Haskell
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.
 

23 lines
722 B

module Bell where
import Data.MemoTrie
import Stirling
-- A000110
-- number of partitions of a set
-- also the number of n-types in the structure (N, =)
bell :: Int -> Integer
bell = memo bell0 where
bell0 n = if n <= 12
then table !! n
else sum [ secondStirling n k | k <- [0 .. n] ]
table = [1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570, 4213597]
-- number of n-types in the structure (N, <=)
orderedBell :: Int -> Integer
orderedBell = memo ob where
ob n = if n <= 10
then table !! n
else sum [ fac k * secondStirling n k | k <- [0 .. n] ]
table = [1, 1, 3, 13, 75, 541, 4683, 47293, 545835, 7087261, 102247563]
fac k = product [1 .. fromIntegral k]