Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ### Project Euler, problem 141 ### --
- import Data.List (sort)
- intToDouble :: Int -> Double
- intToDouble n = fromIntegral n :: Double
- ratio :: (Int, Int, Int) -> Double
- ratio (a, b, c) | 0 `notElem` [a, b, c] = (\(x:y:z:_) -> if x/y == y/z then x/y else 0.0) . sort $ map intToDouble [a, b, c]
- | otherwise = 0.0
- progressive :: Int -> Bool
- progressive n = any (== True) $ map (\d -> ratio (d, n `div` d, n `mod` d) /= 0.0) [2..n `div` 2]
- perfectSquare :: Int -> Bool
- perfectSquare n = (squareRoot n) == (fromIntegral . truncate $ squareRoot n)
- where
- squareRoot = sqrt . fromIntegral
- solveit n = filter (apply3f perfectSquare progressive (&&)) [1..n]
- where
- apply3f f1 f2 f3 x = f3 (f1 x) (f2 x)
- main = do
- putStrLn . show . sum $ solveit 100000
Advertisement
Add Comment
Please, Sign In to add comment