Purple_Rain
Purple_Rain

Reputation: 77

Finding a number that is divisible by all numbers from 1 through 10 evenly

I want to find a number that is divisible by all numbers between 1 and 10. My code sample prints out all the numbers that are divisible. However how would I go about approaching the problem to get just the number that is divisible by ALL numbers from 1 to 10, not just a few:

public class prob_5 {
public static void main(String args[]) {

    int count = 0;

    for (int x = 1; x <= 3000; x++) {
        for (int i = 1; i <= 10; i++) {

            if (x % i == 0) {

                System.out.println(x);
            }

        }
    }

}

}

Upvotes: 2

Views: 6048

Answers (3)

Mohammad Faiz Ahamad
Mohammad Faiz Ahamad

Reputation: 21

package com.test.inter;

/**
 * Class to find the lowest number divisible by 1,2,3...20 * @author Faiz Ahamad
 */
public class TestLowestDivisibleFaiz {

    public static void main(String[] args) {

        int n;
        boolean isDivisible = true;

        for (n = 20; n < 300000000; n++) {

            for (int i = 1; i <= 20; i++) {
                if (n % i != 0) {
                    isDivisible = false;
                    break;
                } else {
                    isDivisible = true;
                }
            }
            // If the number is divisible by all break out of the outer loop to
            // get the exact number
            if (isDivisible)
                break;
        }

        // Print if the number is divisible by all
        if (isDivisible) {
            System.out.println(n);
        }
    }
}

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726669

First, observe that you cannot print inside the inner loop: it is too early. Then figure out when you can print: it's in the outside loop, right after the inner loop.

How do you know that you should print an answer? You need to check that all the MODs of the inner loop have produced no remainder. To do that, add a boolean flag, set it to true outside the inner loop, and set it to false if you see a non-zero remainder.

If your flag survives the inner loop without becoming false, you've got your answer!

boolean isGood = true;
for (int i = 1; i <= 10; i++) {
    if (x % i != 0) {
        isGood = false;
        break;
    }
}
if (isGood) {
    System.out.println(x);
}

Note: of course there is no point in doing all these checks - all you need is to find the first number by deciding on the smallest number of primary factors that you need to multiply together to get a number that is divisible by all ten numbers, and then print all multiples of that number.

Upvotes: 2

DerStrom8
DerStrom8

Reputation: 1341

The least common multiple of the values between 1 and 10 is what you are looking for. To do this, all you have to do is multiply each number by the next, so you would have 1*2*3*4...*9*10. This will always give you value that is divisible by all of the numbers in the range.

EDIT: This is exactly what Oli Charlesworth suggested in his comment--10 factorial (10!) is 10*9*8...*3*2*1, which is the same as what I mentioned earlier.

Upvotes: 3

Related Questions