Phil Choi
Phil Choi

Reputation: 67

Project Euler smallest multiple

The code seems to bee working for 10 but not the 20 as the question asks. Maybe the number is too big when it is divisible by 20...

public static void main(String args[]) {
    long c = 0;
    while(true){
        int x = 0;
        c++;
        for(int i = 1; i<=20; i++){
            x += c%i;
        }
        if(x==0){
            System.out.println(c);
            break;
        }
    }
}

}

No error message, it just continues running without "process finished"

Upvotes: 1

Views: 219

Answers (1)

Abhishek Garg
Abhishek Garg

Reputation: 2288

You are basically trying to find the LCM of 1-20 numbers. The LCM of that is 2,32,792,560. The program will eventually finish but will be very slow to do so.

Sample program to test.

public static void main(String args[]) {
        long c = 232792559;
        while (true) {
            int x = 0;
            c++;
            for (int i = 1; i <= 20; i++) {
                x += c % i;
            }
            if (x == 0) {
                System.out.println(c);
                break;
            }
        }
    }

Output of this will be 232792560

Upvotes: 2

Related Questions