Avatar of JohannesFKnauf

JohannesFKnauf's solution

to Sum Of Multiples in the Elm Track

Published at Sep 23 2019 · 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 first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

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 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 (..)

import List

-- sumOfMultiples leveraging Gauss's trick
-- the sum of all numbers 1..n = n * (n + 1) / 2
-- the sum of the multiples of a single number n until including limit l hence is
--     n, 2*n, ..., n * l // n
--   = n * (sum 1 .. l // n)
--      substituting with Gauss
--   = n * ( (l // n) * (l // n + 1) // 2)
--
-- in case of two or more numbers we have to account for double counting of common multiples after adding the terms for the individual numbers for each pair of numbers
--
-- in case of three or more numbers, we have to account for double removing of common multiples during the double counting correcture for each triple of numbers
--
-- etc. pp.

sumOfMultiples : List Int -> Int -> Int
sumOfMultiples numbers limit =
    numbers
        |> combinations
        |> List.map (gaussTerm limit)
        |> List.sum

-- gaussTerm calculates a single summand in the sum of Gauss terms
gaussTerm : Int -> List Int -> Int
gaussTerm limit combination =
    case combination of
        [] -> 0
        ns ->
            let
                sign = (-1) ^ (1 + List.length ns)
                lcmCombined = List.foldl lcm 1
                factor = lcmCombined ns
                summandCount = (limit - 1) // factor
                total = factor * summandCount * (summandCount + 1) // 2
            in
                sign * total

combinations : List Int -> List (List Int)
combinations elements =
    case elements of
        [] -> [[]]
        x::xs -> List.map ((::) x) (combinations xs) ++ (combinations xs)
                 
lcm : Int -> Int -> Int
lcm a b =
    (a * b) // (gcd a b)

gcd : Int -> Int -> Int
gcd a b =
    case b of
        0 -> a
        otherwise -> gcd b (modBy b a)

Community comments

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

JohannesFKnauf's Reflection

Publishing a solution here that leverages Gauss's trick for calculating sums of a sequence of integers.

This solution works well for large limits, but breaks down soon for large numbers of given factors.