Published at Jul 13 2018
·
1 comment

Instructions

Test suite

Solution

Given a number, find the sum of all the unique multiples of particular numbers up to but not including that number.

If we list all the natural numbers below 20 that are multiples of 3 or 5, we get 3, 5, 6, 9, 10, 12, 15, and 18.

The sum of these multiples is 78.

For installation and learning resources, refer to the exercism help page.

To run the test suite, execute the following command:

```
stack test
```

```
No .cabal file found in directory
```

You are probably running an old stack version and need to upgrade it.

```
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
```

Just do as it says and it will download and install the correct compiler version:

```
stack setup
```

If you want to play with your solution in GHCi, just run the command:

```
stack ghci
```

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

A variation on Problem 1 at Project Euler http://projecteuler.net/problem=1

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE RecordWildCards #-}
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import SumOfMultiples (sumOfMultiples)
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs
specs :: Spec
specs = describe "sumOfMultiples" $ for_ cases test
where
test Case{..} = it description assertion
where
description = unwords [show factors, show limit]
assertion = expression `shouldBe` fromIntegral expected
expression = sumOfMultiples (fromIntegral <$> factors)
(fromIntegral limit )
data Case = Case { factors :: [Integer]
, limit :: Integer
, expected :: Integer
}
cases :: [Case]
cases = [ Case { factors = [3, 5]
, limit = 1
, expected = 0
}
, Case { factors = [3, 5]
, limit = 4
, expected = 3
}
, Case { factors = [3]
, limit = 7
, expected = 9
}
, Case { factors = [3, 5]
, limit = 10
, expected = 23
}
, Case { factors = [3, 5]
, limit = 100
, expected = 2318
}
, Case { factors = [3, 5]
, limit = 1000
, expected = 233168
}
, Case { factors = [7, 13, 17]
, limit = 20
, expected = 51
}
, Case { factors = [4, 6]
, limit = 15
, expected = 30
}
, Case { factors = [5, 6, 8]
, limit = 150
, expected = 4419
}
, Case { factors = [5, 25]
, limit = 51
, expected = 275
}
, Case { factors = [43, 47]
, limit = 10000
, expected = 2203160
}
, Case { factors = [1]
, limit = 100
, expected = 4950
}
, Case { factors = []
, limit = 10000
, expected = 0
}
]
```

```
module SumOfMultiples (sumOfMultiples, sumOfMultiplesDefault) where
sumOfMultiplesDefault :: Int -> Int
sumOfMultiplesDefault = sumOfMultiples [3,5]
sumOfMultiples :: [Int] -> Int -> Int
sumOfMultiples divs limit = sum factors
where
factors = filter (`dividesAny` divs) [1.. pred limit]
dividesAny x = any (divides x)
divides x y = x `mod` y == 0
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,368 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

I'm starting with all numbers and knocking out the non multiples. It's probably more efficent to only generate the multiples, but this seems like the most readable approach.