Challenge: 2520 is the smallest number divisible by 1 to 10 without any remainder.What is the smallest positive number that is evenly divisible by 1-20?Solution:public class Euler5 {    public static void main(String[] args) {        long startTime = System.nanoTime();        int result = 0;        for (int i = 2520; i > 0; i += 2520) {            if (isSmallestMultiple(i)) {                result = i;                break;            }        }        System.out.print(Result:  + result +            .\nTime used for calculation in nanoseconds:  +            (System.nanoTime() - startTime) + .);    }    public static boolean isSmallestMultiple(int i) {        return i % 2 == 0 &&            i % 3 == 0 &&            i % 4 == 0 &&            i % 5 == 0 &&            i % 6 == 0 &&            i % 7 == 0 &&            i % 8 == 0 &&            i % 9 == 0 &&            i % 10 == 0 &&            i % 11 == 0 &&            i % 12 == 0 &&            i % 13 == 0 &&            i % 14 == 0 &&            i % 15 == 0 &&            i % 16 == 0 &&            i % 17 == 0 &&            i % 18 == 0 &&            i % 19 == 0 &&            i % 20 == 0;    }}Example output: Result: 232792560.Time Used for Calculation in nanoseconds: 11561136.This is the first thing I tried. I started with a should work sentiment, and now that it has, I feel like it's an exceptional case.Is incrementing by the given multiple, given its coverage of 1through 10 more intuitive than I suspect? I'm wondering if thissituation is coincidental since they're not all primes.Is there any obvious way of making this even faster?Would this solution work for additional requirements? (i.e 1-25)
Project Euler #5 (LCM of 1-20)
java;algorithm;programming challenge;mathematics
for (int i = 2520; i > 0; i += 2520) and a break statement inside makes the logic of you code hard to follow. The questions are: what is 2520? It is a good practice to give magic constants some meaningful names. The second question is: what does i > 0 mean here? If you know that this condition always holds true, there is no need to keep it. You can either remove it or(even better) rewrite this piece of code using while loop:int result = 2520;while (!isSmallestMultiply(result))    result += 2520;The isSmallestMultiple method could be implemented using a loop. That huge boolean expression is hard to read and it is unnecessary.The algorithm itself is pretty strange(it is not clear why it is correct(because of a magic constant 2520 in the first place), eventhough it is) and it is not efficient. A standard way to find an LCM of a group of numbers is to use the following fact: LCM(a0, a1, a2, ..., an) = LCM(LCM(a0, a1), a2, ..., an). Here is a simple and efficient implementation of it:public class Euler5 {    final static int MAX_N = 20;    static int gcd(int a, int b) {        while (b != 0) {            int t = a;            a = b;            b = t % b;        }        return a;    }    public static void main(String[] args) {        int result = 1;        for (int i = 1; i <= MAX_N; i++) {            int currentGcd = gcd(i, result);            result = result / currentGcd * i;        }        System.out.println(result);    }}