2009년 1월 29일 목요일

프로젝트 오일러 30 haskell 풀이





import Data.Char(digitToInt)

main = print $ sum solve

sumOfPowers :: Int -> Int -> Int
sumOfPowers n = sum . mapDigits (^n)

sumOfFifthPowers = sumOfPowers 5

-- 음.. 어퍼바운드가 역시나 산술적으로 나오는 모양인데.. 난 그냥 적절히
-- 큰수로 박았다. 쩝
solve = filter surprise candidates
where surprise n = sumOfFifthPowers n == n
candidates = [2..999999]

-- 전에 만들어둔 함수. 역시나 오일러에서는 자주쓰이네
mapDigits f n = map (f.digitToInt) (show n)

댓글 없음: