# rootulp's solution

## to Sum Of Multiples in the Elm Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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 Exercism help page for Elm installation and learning resources.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

\$ elm-package install --yes

Execute the tests with:

\$ elm-test

Automatically run tests again when you save changes:

\$ elm-test --watch

As you work your way through the test suite, 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's possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

module Tests exposing (..)

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)
]

### elm-package.json

{
"version": "3.0.0",
"summary": "Exercism problems in Elm.",
"repository": "https://github.com/exercism/elm.git",
"source-directories": [
".",
".."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-community/elm-test": "4.0.0 <= v < 5.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
module SumOfMultiples exposing (..)
import Set

sumOfMultiples: List Int -> Int -> Int
sumOfMultiples numbers maxMultiple =
numbers
|> List.concatMap (\number -> multiplesOf number maxMultiple [])
|> removeDuplicates
|> List.sum

multiplesOf: Int -> Int -> List Int -> List Int
multiplesOf baseNumber maxMultiple multiplesSoFar =
let nextMultiple = calculateNextMultiple baseNumber (List.head multiplesSoFar) in
if nextMultiple < maxMultiple then
multiplesOf baseNumber maxMultiple (nextMultiple :: multiplesSoFar)
else
multiplesSoFar

calculateNextMultiple: Int -> Maybe Int -> Int
calculateNextMultiple baseNumber previousMultiple =
baseNumber + Maybe.withDefault 0 previousMultiple

removeDuplicates: List Int -> List Int
removeDuplicates numbers =
numbers
|> Set.fromList
|> Set.toList