Project Euler Problem 017
-- 英単語を記述していくのもよいが数値のまま計算するのが楽 dig :: Num a => Int -> a dig i = [3,3,5,4,4,3,5,5,4] !! (i-1) -- 1-9 teen :: Num a => Int -> a teen i = [6,6,8,8,7,7,9,8,8] !! (i-11) -- 11-19 tenth :: Num a => Int -> a tenth i = [3,6,6,5,5,5,7,6,6] !! (i-1) -- 10,20,..,90 hundred :: Integer hundred = 7 thousand :: Integer thousand = 11 --onethousand len :: Int -> Integer len 0 = 0 len n | n<10 = dig n len n | n==10 = tenth 1 len n | n<20 = teen n len n | n<100 = tenth(n`quot`10) + len (n`rem`10) len n | n==1000 = thousand len n = len (n`quot`100) + hundred + r where left = len (n`rem`100) r = if left/=0 then 3+left else 0 -- andをつけるかつけないか ans1 :: Integer ans1 = sum [len n|n<-[1..1000]] --21124 main :: IO () main = print ans1