Files
ProjectEulerJava/Problem1.java

54 lines
1.8 KiB
Java

//ProjectEuler/Java/Problem1.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 03-28-19
//What is the sum of all the multiples of 3 or 5 that are less than 1000
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2019 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import mattrixwv.Stopwatch;
public class Problem1{
private static final Integer TOP_NUM = 1000;
public static void main(String[] argv){
Integer sum = 0; //Holds the sum of all the correct elements
Stopwatch timer = new Stopwatch();
//Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum
timer.start();
for(int cnt = 1;cnt < TOP_NUM;++cnt){
if((cnt % 3) == 0){
sum += cnt;
}
else if((cnt % 5) == 0){
sum += cnt;
}
}
timer.stop();
//Print the results
System.out.println("The sum of all numbers < " + TOP_NUM.toString() + " is " + sum.toString());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
}
}
/* Results:
The sum of all numbers < 1000 is 233168
It took 504.226 microseconds to run this algorithm
*/