Project Euler #20: Factorial digit sum

  • + 0 comments

    Haskell

    import Control.Applicative
    import Control.Monad
    import System.IO
    import Data.Char
    
    fact :: Integer -> Integer
    fact 0 = 1
    fact 1 = 1
    fact n = n * fact (n-1)
    
    main :: IO ()
    main = do
        t_temp <- getLine
        let t = read t_temp :: Int
        forM_ [1..t] $ \a0  -> do
            n_temp <- getLine
            let n = read n_temp :: Integer
            let num = fact n
            let num_string = show num
            let digits = map (\x -> digitToInt x) num_string
            print(sum digits)