Fith problem from project Euler.
This is my second attempt. My first attempt took roughly 5 seconds to run, not very good really, I wasn't using the value they gave as a starting point so it was counting to 2520 when it didn't need to... Feedback on better ways to do these are most welcome!
Question:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Answer:
public class problem5 {
public static void main(String[] args){
int current = 2520;
while (true) {
if (isDivisible(current))
break;
current++;
}
System.out.println("Smallest multiple: " + current);
}
public static boolean isDivisible(int num) {
for (int i = 2; i <= 20; i++) {
if (num % i != 0)
return false;
}
return true;
}
}
No comments:
Post a Comment