1
0
Fork 0

Go back to a `sum` in prelude

main
Bill Ewanick 2024-03-15 00:03:31 -04:00
parent e0aab0618b
commit 51ae128a14
1 changed files with 6 additions and 22 deletions

View File

@ -5,9 +5,6 @@ The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use sum" #-}
module Main where
import Data.List (union)
@ -16,26 +13,13 @@ main :: IO ()
main = print ans
ans :: Integer
ans = sum $ allNumbersUnder 1000
sum' :: Num a => [a] -> a
sum' = foldl (+) 0
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
ans = sum (_3s <> _5s)
_3s :: [Integer]
_3s = [3,6..999]
_5s :: [Integer]
_5s = [5,10..999]
solution :: Integer
solution = sum $ union _3s _5s