Skip to content

Instantly share code, notes, and snippets.

@stripe-q
Created September 20, 2017 10:06
Show Gist options
  • Save stripe-q/b8fadb38d246b10d77539f3c595e8eea to your computer and use it in GitHub Desktop.
Save stripe-q/b8fadb38d246b10d77539f3c595e8eea to your computer and use it in GitHub Desktop.
소인수분해정보로 최대공약수찾기 (하스켈)
import Data.List
import Data.Function
type Fac = [(Int, Int)]
list2tuples :: [Int] -> Fac
list2tuples [] = []
list2tuples (a:b:xs) = (a,b):(list2tuples xs)
list2tuples _ = error "Error"
tuples2list = concatMap (\(a, b) -> [a, b])
myGCD :: Fac -> Fac -> Fac
myGCD xs ys = zip cs es
where
cs = (intersect `on` map fst) xs ys
findE c = snd . head . dropWhile ((/= c) . fst)
findMinE c = (min `on` findE c) xs ys
es = map findMinE cs
solve = do
getLine
inputData <- (map (map read . words) . lines) <$> getContents
let result = tuples2list . folr1 myGCD . map list2tuples $ inputData
putStrLn . unwords . map show $ result
main = solve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment