Stirling numbers of the first kind: Difference between revisions

m
Line 479:
import qualified Data.MemoCombinators as Memo
 
sterling1stirling1 :: Integral a => a -> a -> a
sterling1stirling1 = Memo.memo2 Memo.integral Memo.integral f
where
f n k
Line 486:
| n > 0 && k == 0 = 0
| k > n = 0
| otherwise = sterling1stirling1 (pred n) (pred k) + pred n * sterling1stirling1 (pred n) k
 
main :: IO ()
Line 494:
printf "%s\n" $ replicate (13 * 10 + 3) '-'
mapM_ (\row -> printf "%2d|" (fst $ head row) >>
mapM_ (printf "%10d" . uncurry sterling1stirling1) row >> printf "\n") table
printf "\nThe maximum value of S1(100, k):\n%d\n" $
maximum ([sterling1stirling1 100 n | n <- [1..100]] :: [Integer])
where
table :: [[(Int, Int)]]
Line 508:
import Control.Monad.Memo (MonadMemo, memo, startEvalMemo, for2)
 
sterling1stirling1 :: (Integral n, MonadMemo (n, n) n m) => n -> n -> m n
sterling1stirling1 n k
| n == 0 && k == 0 = pure 1
| n > 0 && k == 0 = pure 0
| k > n = pure 0
| otherwise = do
f1 <- for2 memo sterling1stirling1 (pred n) (pred k)
f2 <- for2 memo sterling1stirling1 (pred n) k
pure (f1 + pred n * f2)
 
sterling1Memostirling1Memo :: Integral n => n -> n -> n
sterling1Memostirling1Memo n k = startEvalMemo $ sterling1stirling1 n k
 
main :: IO ()
Line 527:
printf "%s\n" $ replicate (13 * 10 + 3) '-'
mapM_ (\row -> printf "%2d|" (fst $ head row) >>
mapM_ (printf "%10d" . uncurry sterling1Memostirling1Memo) row >> printf "\n") table
printf "\nThe maximum value of S1(100, k):\n%d\n" $
maximum ([sterling1Memostirling1Memo 100 n | n <- [1..100]] :: [Integer])
where
table :: [[(Int, Int)]]
Anonymous user