Avatar of PatrickMcSweeny

PatrickMcSweeny's solution

to Sum Of Multiples in the Ruby Track

Published at Oct 21 2018 · 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.


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

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.

sum_of_multiples_test.rb

require 'minitest/autorun'
require_relative 'sum_of_multiples'

# Common test data version: 1.4.1 8f89751
class SumOfMultiplesTest < Minitest::Test
  def test_no_multiples_within_limit
    # skip
    sum_of_multiples = SumOfMultiples.new(3, 5)
    assert_equal 0, sum_of_multiples.to(1)
  end

  def test_one_factor_has_multiples_within_limit
    skip
    sum_of_multiples = SumOfMultiples.new(3, 5)
    assert_equal 3, sum_of_multiples.to(4)
  end

  def test_more_than_one_multiple_within_limit
    skip
    sum_of_multiples = SumOfMultiples.new(3)
    assert_equal 9, sum_of_multiples.to(7)
  end

  def test_more_than_one_factor_with_multiples_within_limit
    skip
    sum_of_multiples = SumOfMultiples.new(3, 5)
    assert_equal 23, sum_of_multiples.to(10)
  end

  def test_each_multiple_is_only_counted_once
    skip
    sum_of_multiples = SumOfMultiples.new(3, 5)
    assert_equal 2_318, sum_of_multiples.to(100)
  end

  def test_a_much_larger_limit
    skip
    sum_of_multiples = SumOfMultiples.new(3, 5)
    assert_equal 233_168, sum_of_multiples.to(1_000)
  end

  def test_three_factors
    skip
    sum_of_multiples = SumOfMultiples.new(7, 13, 17)
    assert_equal 51, sum_of_multiples.to(20)
  end

  def test_factors_not_relatively_prime
    skip
    sum_of_multiples = SumOfMultiples.new(4, 6)
    assert_equal 30, sum_of_multiples.to(15)
  end

  def test_some_pairs_of_factors_relatively_prime_and_some_not
    skip
    sum_of_multiples = SumOfMultiples.new(5, 6, 8)
    assert_equal 4_419, sum_of_multiples.to(150)
  end

  def test_one_factor_is_a_multiple_of_another
    skip
    sum_of_multiples = SumOfMultiples.new(5, 25)
    assert_equal 275, sum_of_multiples.to(51)
  end

  def test_much_larger_factors
    skip
    sum_of_multiples = SumOfMultiples.new(43, 47)
    assert_equal 2_203_160, sum_of_multiples.to(10_000)
  end

  def test_all_numbers_are_multiples_of_1
    skip
    sum_of_multiples = SumOfMultiples.new(1)
    assert_equal 4_950, sum_of_multiples.to(100)
  end

  def test_no_factors_means_an_empty_sum
    skip
    sum_of_multiples = SumOfMultiples.new()
    assert_equal 0, sum_of_multiples.to(10_000)
  end

  def test_the_only_multiple_of_0_is_0
    skip
    sum_of_multiples = SumOfMultiples.new(0)
    assert_equal 0, sum_of_multiples.to(1)
  end

  def test_solutions_using_include_exclude_must_extend_to_cardinality_greater_than_3
    skip
    sum_of_multiples = SumOfMultiples.new(2, 3, 5, 7, 11)
    assert_equal 39_614_537, 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

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?