thoga31

Investigating progressive numbers (Euler, 141)

Aug 28th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ### Project Euler, problem 141 ### --
  2. import Data.List (sort)
  3.  
  4. intToDouble :: Int -> Double
  5. intToDouble n = fromIntegral n :: Double
  6.  
  7. ratio :: (Int, Int, Int) -> Double
  8. 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]
  9.                 | otherwise = 0.0
  10.  
  11. progressive :: Int -> Bool
  12. progressive n = any (== True) $ map (\d -> ratio (d, n `div` d, n `mod` d) /= 0.0) [2..n `div` 2]
  13.  
  14. perfectSquare :: Int -> Bool
  15. perfectSquare n = (squareRoot n) == (fromIntegral . truncate $ squareRoot n)
  16.   where
  17.     squareRoot = sqrt . fromIntegral
  18.  
  19. solveit n = filter (apply3f perfectSquare progressive (&&)) [1..n]
  20.   where
  21.     apply3f f1 f2 f3 x = f3 (f1 x) (f2 x)
  22.  
  23. main = do
  24.   putStrLn . show . sum $ solveit 100000
Advertisement
Add Comment
Please, Sign In to add comment