1
0
Fork 0
universal-calculator/src/projectEuler/question1.hs

29 lines
771 B
Haskell

{-
https://projecteuler.net/problem=1
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.
-}
module Main where
main :: IO ()
main = print ans
ans :: Integer
ans = sum $ allNumbersUnder 1000
isMultipleOf :: Integral a => a -> a -> Bool
isMultipleOf n x = x `rem` n == 0
all3sUnder :: Integral a => a -> [a]
all3sUnder n = filter (isMultipleOf 3) [1..n-1]
all5sUnder :: Integral a => a -> [a]
all5sUnder n = filter (isMultipleOf 5) [1..n-1]
allXsUnderN :: Integral a => a -> a -> [a]
allXsUnderN x n = filter (isMultipleOf x) [1..n-1]
allNumbersUnder :: Integral a => a -> [a]
allNumbersUnder l = allXsUnderN 3 l <> allXsUnderN 5 l