Project Euler | Problem One
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.
Fairly simple problem in which bruteforcing will take minimal time.
Simply go through every number below 1000 (read: < not <=). For each of the numbers we can test for divisibilty by using the modulus operator. If either returns zero then it is a multiple and we may add it to the list.
/**
* Simple one of course.
* 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.
* @author syfran
* @version %I% %U% %G%
*/
package com.syfran.Euler;
public class ProblemOne extends AbstractEuler
{
private int limit = 1000;
public long solve()
{
int sum = 0;
for(int i = 0; i < limit; i ++)
{
if(i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
return sum;
}
public static void main(String [] wtf)
{
ProblemOne p = new ProblemOne();
p.start();
}
}
Note: the abstractEuler class simply provides the start() class, which prints the results of solve() as well as the runtime. More functionality can be added later.
Total runtime: 1ms
Possible Optimizations:
Could be possible to alternatively add 3 and then 2 to get all the multiples, but due to the very easy nature of this problem and the already 1 ms runtime, this would be overkill.










