mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
70 lines
2.3 KiB
Java
70 lines
2.3 KiB
Java
//ProjectEuler/Java/Problem5.java
|
|
//Matthew Ellison
|
|
// Created: 03-01-19
|
|
//Modified: 03-28-19
|
|
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
|
//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;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
public class Problem5{
|
|
public static void main(String[] argv){
|
|
Stopwatch timer = new Stopwatch(); //So you can time the code
|
|
|
|
//Start the timer
|
|
timer.start();
|
|
|
|
//Start at 20 because it must at least be divisible by 20. Increment by 2 because it must be an even number to be divisible by 2
|
|
Boolean numFound = false;
|
|
Integer currentNum = 20;
|
|
while((currentNum > 0) && (!numFound)){
|
|
//Start by assuming you found the number (because we throw a flag if we didn't find it)
|
|
numFound = true;
|
|
//Step through every number from 1-20 seeing if the current number is divisible by it
|
|
for(Integer divisor = 1;divisor <= 20;++divisor){
|
|
//If it is not divisible then throw a flag and start looking at the next number
|
|
if((currentNum % divisor) != 0){
|
|
numFound = false;
|
|
break;
|
|
}
|
|
}
|
|
//If you didn't find the correct numbe then increment by 2
|
|
if(!numFound){
|
|
currentNum += 2;
|
|
}
|
|
}
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//Print the results
|
|
System.out.println("The smallest positive number evenly divisibly by all number 1-20 is " + currentNum.toString());
|
|
System.out.println("It took " + timer.getStr() + " to run this algorithm");
|
|
}
|
|
}
|
|
|
|
/* Results:
|
|
The smallest positive number evenly divisibly by all number 1-20 is 232792560
|
|
It took 1.987 seconds to run this algorithm
|
|
*/
|