ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Jul 13 2018
·
1 comment

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 exercism help 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.1.0 df076b2
class SumOfMultiplesTest < Minitest::Test
def test_multiples_of_3_or_5_up_to_1
# skip
assert_equal 0, SumOfMultiples.new(3, 5).to(1)
end
def test_multiples_of_3_or_5_up_to_4
skip
assert_equal 3, SumOfMultiples.new(3, 5).to(4)
end
def test_multiples_of_3_up_to_7
skip
assert_equal 9, SumOfMultiples.new(3).to(7)
end
def test_multiples_of_3_or_5_up_to_10
skip
assert_equal 23, SumOfMultiples.new(3, 5).to(10)
end
def test_multiples_of_3_or_5_up_to_100
skip
assert_equal 2_318, SumOfMultiples.new(3, 5).to(100)
end
def test_multiples_of_3_or_5_up_to_1000
skip
assert_equal 233_168, SumOfMultiples.new(3, 5).to(1000)
end
def test_multiples_of_7_13_or_17_up_to_20
skip
assert_equal 51, SumOfMultiples.new(7, 13, 17).to(20)
end
def test_multiples_of_4_or_6_up_to_15
skip
assert_equal 30, SumOfMultiples.new(4, 6).to(15)
end
def test_multiples_of_5_6_or_8_up_to_150
skip
assert_equal 4_419, SumOfMultiples.new(5, 6, 8).to(150)
end
def test_multiples_of_5_or_25_up_to_51
skip
assert_equal 275, SumOfMultiples.new(5, 25).to(51)
end
def test_multiples_of_43_or_47_up_to_10000
skip
assert_equal 2_203_160, SumOfMultiples.new(43, 47).to(10000)
end
def test_multiples_of_1_up_to_100
skip
assert_equal 4_950, SumOfMultiples.new(1).to(100)
end
def test_multiples_of_an_empty_list_up_to_10000
skip
assert_equal 0, SumOfMultiples.new().to(10000)
end
# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
def test_bookkeeping
skip
assert_equal 2, BookKeeping::VERSION
end
end
```

```
class SumOfMultiples
DEFAULT_FACTORS = [3, 5]
def initialize(*factors)
@factors = factors
end
def to(boundary)
multiples = @factors.each_with_object([]) do |factor, multiples|
multiples << (factor...boundary).step(factor).to_a
end
multiples.flatten.uniq.inject(0, :+)
end
def self.to(boundary)
new(*DEFAULT_FACTORS).to(boundary)
end
end
```

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?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

I moved 3 and 5 to a constant to signify their meaning. I don't like the way the tests for this exercise were written. Why even assume that the default behavior should be to sum multiples of 3 and 5? Why those numbers? The existence of the class to method doesn't make any sense to me.