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

rootulp's solution

to Sum Of Multiples in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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 the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

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.

SumOfMultiplesTest.java

import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.*;

public class SumOfMultiplesTest {

    @Test
    public void testSumOfMultiplesOf3and5UpToOne() {

        int[] set = {
                3,
                5
        };
        int output = new SumOfMultiples(1, set).getSum();
        assertEquals(0, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf3and5UpToFour() {

        int[] set = {
                3,
                5
        };
        int output = new SumOfMultiples(4, set).getSum();
        assertEquals(3, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf3aUpToSeven() {

        int[] set = {
                3
        };
        int output = new SumOfMultiples(7, set).getSum();
        assertEquals(9, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf3and5UpToTen() {

        int[] set = {
                3,
                5
        };
        int output = new SumOfMultiples(10, set).getSum();
        assertEquals(23, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf3and5UpToOneHundred() {

        int[] set = {
                3,
                5
        };
        int output = new SumOfMultiples(100, set).getSum();
        assertEquals(2318, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf3and5UpToOneThousand() {

        int[] set = {
                3,
                5
        };
        int output = new SumOfMultiples(1000, set).getSum();
        assertEquals(233168, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf7and13and17UpToTwenty() {

        int[] set = {
                7,
                13,
                17
        };
        int output = new SumOfMultiples(20, set).getSum();
        assertEquals(51, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf4and6UpToFifteen() {

        int[] set = {
                4,
                6
        };
        int output = new SumOfMultiples(15, set).getSum();
        assertEquals(30, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf5and6and8UpToOneHundredFifty() {

        int[] set = {
                5,
                6,
                8
        };
        int output = new SumOfMultiples(150, set).getSum();
        assertEquals(4419, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf5and25UpToFiftyOne() {

        int[] set = {
                5,
                25
        };
        int output = new SumOfMultiples(51, set).getSum();
        assertEquals(275, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOf43and47UpToTenThousand() {

        int[] set = {
                43,
                47
        };
        int output = new SumOfMultiples(10000, set).getSum();
        assertEquals(2203160, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOfOneUpToOneHundred() {

        int[] set = {
                1
        };
        int output = new SumOfMultiples(100, set).getSum();
        assertEquals(4950, output);

    }

    @Ignore("Remove to run test")
    @Test
    public void testSumOfMultiplesOfNoneUpToTenThousand() {

        int[] set = {};
        int output = new SumOfMultiples(10000, set).getSum();
        assertEquals(0, output);

    }
}
import java.util.HashSet;
import java.util.Set;

class SumOfMultiples {

  private int number;
  private int[] set;

  SumOfMultiples(int number, int[] set) {
    this.number = number;
    this.set = set;
  }

  int getSum() {
    Set<Integer>  multiplesOfSetOfNumbersUpToMaxNumber = multiplesOfSetOfNumbersUpToMaxNumber(number, set);
    return multiplesOfSetOfNumbersUpToMaxNumber.stream().mapToInt(Integer::intValue).sum();
  }

  private Set<Integer> multiplesOfSetOfNumbersUpToMaxNumber(int maxNumber, int[] set) {
    Set<Integer> multiples = new HashSet<>();

    for(int number : set) {
      multiples.addAll(multiplesOfNumberUpToMaxNumber(maxNumber, number));
    }

    return multiples;
  }

  private Set<Integer> multiplesOfNumberUpToMaxNumber(int maxNumber, int number) {
    Set<Integer> multiples = new HashSet<>();

    int multiplier = 1;
    while (number * multiplier < maxNumber) {
      multiples.add(number * multiplier);
      multiplier += 1;
    }

    return multiples;
  }

}

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?