Skip to content

Instantly share code, notes, and snippets.

@rubik
Created February 10, 2015 13:58
Show Gist options
  • Save rubik/47436a726143393e3d20 to your computer and use it in GitHub Desktop.
Save rubik/47436a726143393e3d20 to your computer and use it in GitHub Desktop.
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