Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of thekeele

thekeele's solution

to Sum Of Multiples in the Elixir Track

Published at May 29 2019 · 0 comments
Test suite


This exercise has changed since this solution 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.

Running tests

Execute the tests with:

$ elixir sum_of_multiples_test.exs

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.


if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("sum_of_multiples.exs", __DIR__)

ExUnit.configure(exclude: :pending, trace: true)

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
defmodule SumOfMultiples do
  @spec to(non_neg_integer, [non_neg_integer]) :: non_neg_integer

  def to(limit, factors) do
    Enum.reduce(2..(limit - 1), 0, fn number, sum ->
      multipe? = Enum.any?(factors, &(rem(number, &1) == 0))
      if multipe?, do: sum + number, else: sum

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?