Avatar of nicolemon

nicolemon's solution

to Sum Of Multiples in the Haskell Track

Published at Aug 14 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution 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.

Getting Started

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

Running the tests

To run the test suite, execute the following command:

stack test

If you get an error message like this...

No .cabal file found in directory

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

Otherwise, if you get an error message like this...

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

Running GHCi

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

stack ghci

Feedback, Issues, Pull Requests

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!

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.hs

{-# 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) where

import Data.List (union)
import qualified Data.Set as S

sumOfMultiples :: [Integer] -> Integer -> Integer
sumOfMultiples [] _ = 0
sumOfMultiples _ 0 = 0
sumOfMultiples factors limit = (sum . nub' . concat) $ map multiples factors
    where multiples n = filter (/= limit) $ map (*n) [1..div limit n]

-- S.fromList is O(n*log n), S.toList is O(n)
-- improvement over Data.List.nub's O(n^2)
nub' :: (Ord a) => [a] -> [a]
nub' = S.toList . S.fromList

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?