Stirling numbers of the first kind: Difference between revisions

m
Line 501:
table = groupBy (\a b -> fst a == fst b) $ (,) <$> [0..12] <*> [0..12]</lang>
 
Or library: monad-memo for memoization. Seems to be a few milliseconds slower.
<lang haskell>{-# LANGUAGE FlexibleContexts #-}
 
import Text.Printf (printf)
import Data.List (groupBy)
import Control.Monad.Memo (MonadMemo, memo, startEvalMemo, for2)
 
stirling1 :: (Integral n, MonadMemo (n, n) n m) => (n ->, n) -> m n
stirling1 (n, k)
| n == 0 && k == 0 = pure 1
| n > 0 && k == 0 = pure 0
| k > n = pure 0
| otherwise = do
f1 <- for2 memo stirling1 (pred n), (pred k)
f2 <- for2 memo stirling1 (pred n), k)
pure (f1 + pred n * f2)
 
stirling1Memo :: Integral n => (n ->, n) -> n
stirling1Memo n k = startEvalMemo $. stirling1 n k
 
main :: IO ()
main = do
printf "n/k"
mapM_ (printf "%10d") ([0..12] :: [Int]) >> printf "\n"
printf "%s\n" $ replicate (13 * 10 + 3) '-'
mapM_ (\row -> printf "%2d|" (fst $ head row) >>
mapM_ (printf "%10d" . uncurry stirling1Memo) row >> printf "\n") table
printf "\nThe maximum value of S1(100, k):\n%d\n" $
maximum ([stirling1Memo (100, n) | n <- [1..100]] :: [Integer])
where
table :: [[(Int, Int)]]
Anonymous user