🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of angelikatyborska

angelikatyborska's solution

to Sum Of Multiples in the Erlang Track

Published at Jan 22 2020 · 0 comments
Instructions
Test suite
Solution

Note:

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

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit

Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

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_tests.erl

%% Based on canonical data version 1.5.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/sum-of-multiples/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(sum_of_multiples_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").




'1_no_multiples_within_limit_test'() ->
    ?assertEqual(0, sum_of_multiples:sum([3, 5], 1)).

'2_one_factor_has_multiples_within_limit_test'() ->
    ?assertEqual(3, sum_of_multiples:sum([3, 5], 4)).

'3_more_than_one_multiple_within_limit_test'() ->
    ?assertEqual(9, sum_of_multiples:sum([3], 7)).

'4_more_than_one_factor_with_multiples_within_limit_test'() ->
    ?assertEqual(23, sum_of_multiples:sum([3, 5], 10)).

'5_each_multiple_is_only_counted_once_test'() ->
    ?assertEqual(2318, sum_of_multiples:sum([3, 5], 100)).

'6_a_much_larger_limit_test'() ->
    ?assertEqual(233168,
		 sum_of_multiples:sum([3, 5], 1000)).

'7_three_factors_test'() ->
    ?assertEqual(51, sum_of_multiples:sum([7, 13, 17], 20)).

'8_factors_not_relatively_prime_test'() ->
    ?assertEqual(30, sum_of_multiples:sum([4, 6], 15)).

'9_some_pairs_of_factors_relatively_prime_and_some_not_test'() ->
    ?assertEqual(4419,
		 sum_of_multiples:sum([5, 6, 8], 150)).

'10_one_factor_is_a_multiple_of_another_test'() ->
    ?assertEqual(275, sum_of_multiples:sum([5, 25], 51)).

'11_much_larger_factors_test'() ->
    ?assertEqual(2203160,
		 sum_of_multiples:sum([43, 47], 10000)).

'12_all_numbers_are_multiples_of_1_test'() ->
    ?assertEqual(4950, sum_of_multiples:sum([1], 100)).

'13_no_factors_means_an_empty_sum_test'() ->
    ?assertEqual(0, sum_of_multiples:sum([], 10000)).

'14_the_only_multiple_of_0_is_0_test'() ->
    ?assertEqual(0, sum_of_multiples:sum([0], 1)).

'15_the_factor_0_does_not_affect_the_sum_of_multiples_of_other_factors_test'() ->
    ?assertEqual(3, sum_of_multiples:sum([3, 0], 4)).

'16_solutions_using_include_exclude_must_extend_to_cardinality_greater_than_3_test'() ->
    ?assertEqual(39614537,
		 sum_of_multiples:sum([2, 3, 5, 7, 11], 10000)).
-module(sum_of_multiples).

-export([sum/2]).


sum(Factors, Limit) ->
  Multiples = lists:flatmap(fun(Factor) -> multiples(Factor, Limit) end, Factors),
  UniqueMultiples = unique(Multiples),
  lists:sum(UniqueMultiples).

multiples(0, _) -> [];
multiples(Factor, Limit) ->
  lists:seq(Factor, Limit - 1, Factor).

unique(List) ->
  Set = sets:from_list(List),
  sets:to_list(Set).

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?