Project Euler Problem 5
http://projecteuler.net/index.php?section=problems&id=5
1から20全てで割り切れる最小の数
--時間かかりすぎ main = print $ head $ filter (isCommonMultiples [1..20]) [21..] isCommonMultiples :: [Int] -> Int -> Bool isCommonMultiples [] _ = True isCommonMultiples (x:xs) n = if (n `rem` x) == 0 then isCommonMultiples xs n else False
ユークリッドの互除法で解いてみようとしたらMain.lcmとPrelude.lcmがダブりってエラーが発生したのでPrelude.lcmを使用
main = print $ foldl lcm 1 [1..20]
以下は自分で書いたgcd,lcm
_gcd :: Integer -> Integer -> Integer _gcd x 0 = x _gcd x y = _gcd y (x `rem` y) _lcm :: Integer -> Integer -> Integer _lcm x y = (x*y) `div` (_gcd x y)