Avatar of paulfioravanti

paulfioravanti's solution

to Sum Of Multiples in the Elixir Track

Published at Jun 25 2019 · 0 comments
Test suite

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.

Running tests

Execute the tests with:

$ mix test

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.


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.


defmodule SumOfMultiplesTest do
  use ExUnit.Case

  # @tag :pending
  test "sum to 1" do
    assert SumOfMultiples.to(1, [3, 5]) == 0

  @tag :pending
  test "sum to 3" do
    assert SumOfMultiples.to(4, [3, 5]) == 3

  @tag :pending
  test "sum to 10" do
    assert SumOfMultiples.to(10, [3, 5]) == 23

  @tag :pending
  test "sum to 20" do
    assert SumOfMultiples.to(20, [3, 5]) == 78

  @tag :pending
  test "sum to 100" do
    assert SumOfMultiples.to(100, [3, 5]) == 2318

  @tag :pending
  test "sum to 1000" do
    assert SumOfMultiples.to(1000, [3, 5]) == 233_168

  @tag :pending
  test "configurable 7, 13, 17 to 20" do
    multiples = [7, 13, 17]
    assert SumOfMultiples.to(20, multiples) == 51

  @tag :pending
  test "configurable 4, 6 to 15" do
    multiples = [4, 6]
    assert SumOfMultiples.to(15, multiples) == 30

  @tag :pending
  test "configurable 5, 6, 8 to 150" do
    multiples = [5, 6, 8]
    assert SumOfMultiples.to(150, multiples) == 4419

  @tag :pending
  test "configurable 43, 47 to 10000" do
    multiples = [43, 47]
    assert SumOfMultiples.to(10000, multiples) == 2_203_160


ExUnit.configure(exclude: :pending, trace: true)
defmodule SumOfMultiples do
  @doc """
  Adds up all numbers from 1 to a given end number that are multiples of the factors provided.
  @spec to(non_neg_integer, [non_neg_integer]) :: non_neg_integer
  def to(limit, factors) do
    1..(limit - 1)
    |> Stream.filter(&multiples?(factors, &1))
    |> Enum.sum()

  defp multiples?(factors, range_number) do
    Enum.any?(factors, &multiple?(range_number, &1))

  defp multiple?(range_number, factor), do: rem(range_number, factor) == 0

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?