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.

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

```
gem install minitest
```

If you would like color output, you can `require 'minitest/pride'`

in
the test file, or note the alternative instruction, below, for running
the test file.

Run the tests from the exercise directory using the following command:

```
ruby sum_of_multiples_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride sum_of_multiples_test.rb
```

A variation on Problem 1 at Project Euler http://projecteuler.net/problem=1

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
require 'minitest/autorun'
require_relative 'sum_of_multiples'
# Common test data version: 1.2.0 fb5b0a1
class SumOfMultiplesTest < Minitest::Test
def test_multiples_of_3_or_5_up_to_1
# skip
sum_of_multiples = SumOfMultiples.new(3, 5)
assert_equal 0, sum_of_multiples.to(1)
end
def test_multiples_of_3_or_5_up_to_4
skip
sum_of_multiples = SumOfMultiples.new(3, 5)
assert_equal 3, sum_of_multiples.to(4)
end
def test_multiples_of_3_up_to_7
skip
sum_of_multiples = SumOfMultiples.new(3)
assert_equal 9, sum_of_multiples.to(7)
end
def test_multiples_of_3_or_5_up_to_10
skip
sum_of_multiples = SumOfMultiples.new(3, 5)
assert_equal 23, sum_of_multiples.to(10)
end
def test_multiples_of_3_or_5_up_to_100
skip
sum_of_multiples = SumOfMultiples.new(3, 5)
assert_equal 2_318, sum_of_multiples.to(100)
end
def test_multiples_of_3_or_5_up_to_1000
skip
sum_of_multiples = SumOfMultiples.new(3, 5)
assert_equal 233_168, sum_of_multiples.to(1_000)
end
def test_multiples_of_7_13_or_17_up_to_20
skip
sum_of_multiples = SumOfMultiples.new(7, 13, 17)
assert_equal 51, sum_of_multiples.to(20)
end
def test_multiples_of_4_or_6_up_to_15
skip
sum_of_multiples = SumOfMultiples.new(4, 6)
assert_equal 30, sum_of_multiples.to(15)
end
def test_multiples_of_5_6_or_8_up_to_150
skip
sum_of_multiples = SumOfMultiples.new(5, 6, 8)
assert_equal 4_419, sum_of_multiples.to(150)
end
def test_multiples_of_5_or_25_up_to_51
skip
sum_of_multiples = SumOfMultiples.new(5, 25)
assert_equal 275, sum_of_multiples.to(51)
end
def test_multiples_of_43_or_47_up_to_10000
skip
sum_of_multiples = SumOfMultiples.new(43, 47)
assert_equal 2_203_160, sum_of_multiples.to(10_000)
end
def test_multiples_of_1_up_to_100
skip
sum_of_multiples = SumOfMultiples.new(1)
assert_equal 4_950, sum_of_multiples.to(100)
end
def test_multiples_of_an_empty_list_up_to_10000
skip
sum_of_multiples = SumOfMultiples.new()
assert_equal 0, sum_of_multiples.to(10_000)
end
end
```

```
class SumOfMultiples
def initialize(*numbers)
@numbers = numbers
end
def to(limit)
@limit = limit
natural_numbers.sum
end
private
attr_reader :numbers, :limit
def range
(1...limit)
end
def natural_numbers
range.select do |number|
numbers.any? do |n|
(number % n).zero?
end
end
end
end
```

A huge amount can be learnt 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 I could read more about to develop my understanding?