LoginSignup
1
0

More than 5 years have passed since last update.

Project EulerをHaskellで解いていく(Problem1: Multiples of 3 and 5)

Last updated at Posted at 2019-02-20

TL;DR

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

問題文

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

10未満の自然数のうち, 3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり, これらの合計は 23 になる.
同じようにして, 1000 未満の 3 か 5 の倍数になっている数字の合計を求めよ.

コード

f :: Int -> Int
f 0 = 0
f n
  | n `mod` 3 == 0 = n + f(n-1)
  | n `mod` 5 == 0 = n + f(n-1)
  | otherwise = f(n-1)

main::IO()
main = do
  print $ f(999)
1
0
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
1
0