wvogel日記

自分用の技術備忘録が多めです.

Project Euler 42(3)

filter関数利用に書き直しただけ

import Data.Char
import Data.Maybe

alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
table = zip alphabets [1..]
trinum = map trifunc [1..]

euler42 :: String -> Bool
euler42 str = wordvalue str `elem` (takeWhile (<=wordvalue str) trinum)

trifunc :: Integral a=>a ->a
trifunc n = n*(n+1) `div` 2

check42 :: Int -> [Int]->Bool
check42 n (x:xs) | n < x = False
                 | n == x = True
                 |otherwise = check42 n xs

wordvalue :: [Char] ->Int
wordvalue [] = 0
wordvalue (s:str) =(fromJust $ lookup (toUpper s) table) + wordvalue str

main =do fs <- readFile "word.txt"
         print $ length.filter euler42$ lines fs