Problem 13

今日は Problem 13 をやってみた。これは簡単。

cf. Project Euler - Problem 13

 nums :: [Integer]
 nums = [37107287533902102798797998220837590246510135740250,
         46376937677490009712648124896970078050417018260538,
         74324986199524741059474233309513058123726617309629,

         ...

         72107838435069186155435662884062257473692284509516,
         20849603980134001723930671666823555245252804609722,
         53503534226472524250874054075591789781264330331690]
 
 euler013 :: String
 euler013 = take 10 $ show $ sum nums
 
 main :: IO ()
 main = putStrLn euler013
 ^o^ >runhaskell euler013.hs
 5537376230

追記:

数字をファイル(euler013.txt)から読み込むようにしてみた。

 euler013 :: [Integer] -> String
 euler013 = take 10 . show . sum
 
 main :: IO ()
 main = do nums <- getContents >>= return . lines
           putStrLn $ euler013 $ map read nums
 ^o^ >runhaskell euler013a.hs < euler013.txt
 5537376230