I
I
Ivan Yakushenko2019-03-17 16:32:16
Java
Ivan Yakushenko, 2019-03-17 16:32:16

Why does the program stop at 25?

public class LargestSimpleDivider {
    public static void main(String[] args) {
        for (long i = 100; i > 0; i--) {
            long number = 100;
            if (i % 2 == 0 || i % 3 == 0) {
                System.out.printf("\nnot like this " + i);
            } else if(number % i == 0) {
                System.out.printf("\nThe largest simple divider: " + i);
                break;
            }
        }
        //System.out.printf("proverka 25: " + 25 % 2 + "\n" + 25 % 3);
    }
}

For some reason, the number 25 passes the first if (i % 2 == 0 || i % 3 == 0) test, even though it shouldn't.
Just in case, I myself checked a little lower - the remainder of the division will be 1, but not 0, so the check should not pass. What's wrong with the number 25?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-03-17
@kshnkvn

It fails this test. It goes through the following if(number % i == 0)
You are looking for the maximum number that is not divisible by 2 and 3 and is a divisor of 100. And then break is triggered, so the program ends at 25.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question