Created
February 10, 2015 13:58
-
-
Save rubik/47436a726143393e3d20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main | |
where | |
perfectSquares :: [Int] | |
perfectSquares = map (^2) [1..100] | |
divisors :: Int -> [(Int, Int)] | |
divisors n = [ (i, div n i) | i <- [1..floor $ sqrt $ fromIntegral n] | |
, mod n i == 0] | |
transformPairs :: [(Int, Int)] -> [(Int, Int)] | |
transformPairs = map (\(a, b) -> ((a + b) `div` 2, (b - a) `div` 2)) | |
isqrt :: Int -> Int | |
isqrt = floor . sqrt . fromIntegral | |
solve :: Int -> [(Int, Int)] | |
solve n = [ (x, isqrt y) | (x, y) <- transformPairs $ divisors n | |
, y `elem` perfectSquares] | |
main :: IO () | |
main = print $ solve 336 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment