Project Euler #5:
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?
Here is my solution:
class SmallestMultiple {
private static final int MAX = 20;
public static void main(String[] args) {
long time = System.nanoTime();
int result = 1;
for(int i = 1; i <= MAX; i++) {
result = smallestMultiple(result, i);
}
time = System.nanoTime() - time;
System.out.println("Result: " + result + "\nTime used to calculate in nanoseconds: " + time);
}
private static int smallestMultiple(int i, int j) {
for(int index = 2; index <= i && index <= j; index++) {
if(i % index == 0 && j % index == 0) {
i /= index;
}
}
return i * j;
}
}
Output:
Result: 232792560
Time used to calculate in nanoseconds: 11603
Questions:
- Is is as efficient as it can be? If not, how can I improve it?
- Does it smell?