Skip to content

Instantly share code, notes, and snippets.

@rfaisal
Created July 5, 2013 17:12
Show Gist options
  • Save rfaisal/5935924 to your computer and use it in GitHub Desktop.
Save rfaisal/5935924 to your computer and use it in GitHub Desktop.
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
public class MultipleOf3And5 {
private int max;
public MultipleOf3And5(int max){
this.max=max;
}
public int calculate(){
int sum=0;
int j=1;
int i=15;
while(i<max){
sum+=i;
i=15*++j;
}
j=1;
i=3;
while(i<max){
if(i%15!=0)sum+=i;
i=3*++j;
}
j=1;
i=5;
while(i<max){
if(i%15!=0)sum+=i;
i=5*++j;
}
return sum;
}
}
public class MultipleOf3And5Test {
@Test
public void testCalculate() {
assertEquals(23, new MultipleOf3And5(10).calculate());
assertEquals(233168,new MultipleOf3And5(1000).calculate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment