 # agbell's solution

## to Sum Of Multiples in the Haskell Track

Published at Jul 13 2018 · 1 comment
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.

## 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  = 
, 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  = 
, limit    = 100
, expected = 4950
}
, Case { factors  = []
, limit    = 10000
, expected = 0
}
]``````
``````module SumOfMultiples (sumOfMultiples, sumOfMultiplesDefault) where

sumOfMultiplesDefault :: Int -> Int
sumOfMultiplesDefault = sumOfMultiples [3,5]

sumOfMultiples :: [Int] -> Int -> Int
sumOfMultiples divs limit = sum factors
where
factors = filter (`dividesAny` divs) [1.. pred limit]
dividesAny x = any (divides x)
divides x y = x `mod` y == 0`````` Solution Author
commented over 5 years ago

I'm starting with all numbers and knocking out the non multiples. It's probably more efficent to only generate the multiples, but this seems like the most readable approach.

### 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?