# artemkorsakov's solution

## to Sum Of Multiples in the Java Track

Published at Feb 18 2019 · 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.

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## 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 testNoMultiplesWithinLimit() {

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

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

}

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

int[] set = {
2,
3,
5,
7,
11
};
int output = new SumOfMultiples(10000, set).getSum();
assertEquals(39614537, output);

}

}``````
``````import java.util.Arrays;
import java.util.stream.IntStream;

class SumOfMultiples {
private int sum;

SumOfMultiples(int number, int[] set) {
sum = IntStream.rangeClosed(1, number - 1).filter(n -> isMultiple(n, set)).sum();
}

int getSum() {
return sum;
}

private boolean isMultiple(int number, int[] set) {
return Arrays.stream(set).anyMatch(s -> s != 0 && number % s == 0);
}
}``````