Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- getExactDiff :: Int -> Int -> [Int]
- getExactDiff n quad = [floor d, ceiling d]
- where
- d = fromIntegral ( abs (n - quad*quad) ) ** 0.5
- compareVar :: (Int, Int, Int) -> (Int, Int, Int) -> Ordering
- compareVar (_, q1, a1) (_, q2, a2)
- | abs(a1) /= abs(a2) = compare (abs a1) (abs a2)
- | otherwise = compare q1 q2
- getPairs :: Int -> [(Int, Int, Int)]
- getPairs n =
- sortBy compareVar [(p, q, p*p+q*q-n) | p <- [1..n_root], q <- (getExactDiff n p), p <= q, q > 0]
- where
- n_root = ceiling (fromIntegral n ** 0.5)
- getVariant :: Int -> (Int, Int, Int)
- getVariant = head . getPairs
- main = do
- putStrLn "Enter number:"
- num <- getLine
- let result = getVariant (read num)
- putStrLn (show result)
Advertisement
Add Comment
Please, Sign In to add comment