Avatar of crewstad

crewstad's solution

to Sum Of Multiples in the Elm Track

Published at Jun 11 2020 · 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)
        ]

src/SumOfMultiples.elm

module SumOfMultiples exposing (sumOfMultiples)

import Arithmetic

-- calculate Least Common Multiple (LCM)
lcm : Int -> Int -> Int
lcm a b =
    if b > 0 then
        -(Arithmetic.lcm a b)
    else
        Arithmetic.lcm a -b

-- compute all LCMs between a number and a list of numbers
computeLCMs : Int -> List Int -> List Int -> List Int
computeLCMs number list acc =
    case list of
        [] ->
            acc
        other::others ->                   
            computeLCMs number others (acc ++ [lcm number other])

-- add LCMs of divisors to the list of divisors (could be optimized by removing +/- pairs)
addLCMs : List Int -> List Int -> List Int               
addLCMs numbers acc =
    let
        aggregateLCMs number list =
            list ++ [number] ++ (computeLCMs number list [])
    in 
        case numbers of
            [] ->
                acc
            n::others ->
                addLCMs others (aggregateLCMs n acc)

-- add sums of divisors and their LCMs multiples
sumMyMultiples : List Int -> Int -> Int
sumMyMultiples numbers limit =    
    case numbers of
        [] ->
            0
        a::others ->
            let
               n = (limit - 1) // (abs a)
            in
               (a * n * (n + 1) // 2) + (sumMyMultiples others limit)
         
sumOfMultiples : List Int -> Int -> Int
sumOfMultiples divisors limit =
    sumMyMultiples (addLCMs divisors []) limit

elm.json

{
    "type": "application",
    "source-directories": [
        "src"
    ],
    "elm-version": "0.19.1",
    "dependencies": {
        "direct": {
            "elm/browser": "1.0.0",
            "elm/core": "1.0.0",
            "elm/html": "1.0.0",
            "elm/regex": "1.0.0",
            "lynn/elm-arithmetic": "3.0.0"
        },
        "indirect": {
            "elm/json": "1.0.0",
            "elm/time": "1.0.0",
            "elm/url": "1.0.0",
            "elm/virtual-dom": "1.0.0"
        }
    },
    "test-dependencies": {
        "direct": {
            "elm-explorations/test": "1.0.0"
        },
        "indirect": {
            "elm/random": "1.0.0"
        }
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?