LoginSignup
2
1

More than 5 years have passed since last update.

Project EulerをHaskellで解いていく(Problem5: Smallest multiple)

Posted at

TL;DR

Haskellの勉強を兼ねてProject Eulerを解いていきます。
始めたばかりでわからないことが多いのでコメント頂けると嬉しいです。

問題文

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

2520 は 1 から 10 の数字の全ての整数で割り切れる数字であり, そのような数字の中では最小の値である.
では, 1 から 20 までの整数全てで割り切れる数字の中で最小の正の数はいくらになるか.

コード

isDivisible::Int -> Bool
isDivisible n = all (==True) [n `mod` x == 0 | x <- [11..20]]

main::IO()
main = do
  print $ (2+) $ maximum $ takeWhile (\x -> not $ isDivisible(x)) [x | x <- [2520,2522..]]

めちゃくちゃ遅いからどうにかしたい…

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1