ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# marcmartino's solution

## to Sum Of Multiples in the Elm Track

Published at Feb 12 2021 · 0 comments
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.

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The code you have to write is located inside the `src/` directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

``````\$ elm-test
``````

To automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the tests suite in the file `tests/Tests.elm`, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

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

## Submitting Incomplete Solutions

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

### Tests.elm

``````module Tests exposing (tests)

import Expect
import SumOfMultiples exposing (sumOfMultiples)
import Test exposing (..)

tests : Test
tests =
describe "Sum Of Multiples"
[ test "[3, 5] 15" <|
\() -> Expect.equal 45 (sumOfMultiples [ 3, 5 ] 15)
, skip <|
test "[7, 13, 17] 20" <|
\() -> Expect.equal 51 (sumOfMultiples [ 7, 13, 17 ] 20)
, skip <|
test "[4, 6] 15" <|
\() -> Expect.equal 30 (sumOfMultiples [ 4, 6 ] 15)
, skip <|
test "[5, 6, 8] 150" <|
\() -> Expect.equal 4419 (sumOfMultiples [ 5, 6, 8 ] 150)
, skip <|
test "[43, 47] 10000" <|
\() -> Expect.equal 2203160 (sumOfMultiples [ 43, 47 ] 10000)
, skip <|
test "[5, 25] 51" <|
\() -> Expect.equal 275 (sumOfMultiples [ 5, 25 ] 51)
]``````
``````module SumOfMultiples exposing (sumOfMultiples)

multsUpTo : Int -> Int -> List Int
multsUpTo max mult =
mult
|> (//) (max - 1)
|> List.range 1
|> List.map ((*) mult)

-- sumOfMultiples : List Int -> Int -> Int
-- sumOfMultiples divisors limit =
--     List.concatMap (multsUpTo limit) divisors
--         |> Set.fromList
--         |> Set.toList
--         |> List.sum

notDivBy : Int -> Int -> Bool
notDivBy x =
modBy x >> (/=) 0

and : (a -> Bool) -> (a -> Bool) -> a -> Bool
and pred1 pred2 x =
pred1 x && pred2 x

genNums : (Int -> Bool) -> List Int -> Int -> List Int
genNums numCondition divisors limit =
case divisors of
div :: restDivs ->
(multsUpTo limit div |> List.filter numCondition)
++ genNums (and numCondition (notDivBy div)) restDivs limit

[] ->
[]

sumOfMultiples : List Int -> Int -> Int
sumOfMultiples divisors limit =
genNums (always True) divisors limit
|> List.sum``````