Java Implementation of Problem 1

View source code here on GitHub!

public class p0001 implements IEuler
Object answer()
Returns:

The answer to Project Euler problem 1

 1/*
 2Project Euler Problem 1
 3
 4I know that this could be done faster with a traditional for loop, but I wanted
 5to see if iterators were reasonably possible in C, since it makes the prime
 6number infrastructure a lot easier to set up.
 7
 8Problem:
 9
10If we list all the natural numbers below 10 that are multiples of 3 or 5, we
11get 3, 5, 6 and 9. The sum of these multiples is 23.
12
13Find the sum of all the multiples of 3 or 5 below 1000.
14*/
15package euler;
16
17public class p0001 implements IEuler {
18    @Override
19    public Object answer() {
20        int answer = 0;
21        for (int i = 0; i < 1000; i += 3)
22            answer += i;
23
24        for (int i = 0; i < 1000; i += 5)
25            answer += i;
26
27        for (int i = 0; i < 1000; i += 15)
28            answer -= i;
29
30        return answer;
31    }
32}

Tags: divisibility