Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

optimization - Could my solution to the 5th Euler Project(Java) problem(LCM of 1 through 20) be optimized?

I have been going through the Euler project problems one by one every day and asking whatever questions I have on this website. The problem description is commented out in the code. This time around, I don't have any particular problems with my code, but I was wondering if there is any way to improve or optimize it. I am a beginner, so it would be helpful to hear any suggestions. Here is the code in Java:

    //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?
    int smallest = 0;
    for(int num = 2520; num < 1000000000; num += 2520) {
        boolean isMultiple = true;
        for (int multiple = 20; multiple >= 11; multiple--) {
            if (num % multiple != 0) {
                isMultiple = false;
                break;
            }
        }
        if (isMultiple) {
            smallest = num;
            break;
        }
    }
    System.out.println("The LCM of all the numbers between one and twenty is : " + smallest);
question from:https://stackoverflow.com/questions/65867778/could-my-solution-to-the-5th-euler-projectjava-problemlcm-of-1-through-20-be

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...