Project EulerをHaskellで解いていく(Problem1: Multiples of 3 and 5)
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)
Thanks for reading!