Project Euler problem one
I am using Project Euler to test myself on the Java programming that I am teaching myself and here is problem one: 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.
Here is my Java attempt at calculating it
Code :
public class problemOne {
public static void main(String[] args) {
int count;
int sum = 0;
//Multiples of 3
for(count = 0; count < 1000; count += 3){
System.out.println("count is: " + count);
sum = sum + count;
}
//Multiples of 5
for(count = 0; count < 1000; count += 5){
System.out.println("count is: " + count);
sum = sum + count;
}
System.out.println("Total is: " + sum);
}
}
I get 26333 as the answer but when I enter that at projecteuler.net it says incorrect.
Is there an error in the logic that I used to calculate? If so, where is it so I can rethink it.
Re: Project Euler problem one
You're forgetting that some numbers are multiples of *both* 3 and 5, and you are in effect adding these numbers *twice*.
Re: Project Euler problem one
Quote:
Originally Posted by
curmudgeon
You're forgetting that some numbers are multiples of *both* 3 and 5, and you are in effect adding these numbers *twice*.
Thanks
Re: Project Euler problem one
I got it figured out! Now on to problem 2...