1

Sorry to ask such a basic question but other questions on here don't seem to fix the problem and I've been staring at it for quite a while now. I'm writing some code to find the smallest common multiple for the numbers from 1 to 20. From debugging, the outer for loop only runs once and I can't figure out why. Can someone please point out where I've gone code blind.

public class ED5 {
public static void main(String[] args){
    int smallestCommonMultiple = 1;
    //excluding 1 because it has no effect on the result and would just mean extra work
    int[] numbers = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    //initially set to true for the first loop
    boolean iIsFactor = true;

    //for each potential divisor from 2-20 (note only the prime divisors will ever cause a division)
    for(int i = 2; i < 21; i++){
        while(iIsFactor){
            //reset flag for the start of each new run through the array
            iIsFactor = false;

            //for each element of the array
            for(int j=0; j<19; j++){
                //if the current divisor is a factor of that array entry
                if(numbers[j]% i == 0){
                    //divide the current entry by the current divisor
                    numbers[j] = numbers[j]/i;
                    //indicate that at least one entry had a factor of i
                    iIsFactor= true;
                }
            }//end for loop for each array pass

            if(iIsFactor){
                smallestCommonMultiple *= i;
            }
        }//end while loop for the divisor
    }//end for loop for all the divisors

    //output result
    System.out.println("The smallest common multiple of the numbers from 1 to 20 is:");
    System.out.println(smallestCommonMultiple);
}

}

5
  • 1
    What makes you think so? Runs 19 times for me. Commented Jun 16, 2015 at 15:25
  • Sysout prints only once... Commented Jun 16, 2015 at 15:25
  • I've tried it multiple times and stepped through with the debugger and the outer for loop only runs once. The print is only supposed to occur once. Commented Jun 16, 2015 at 15:31
  • 1
    You mean for(int i = 2; i < 21; i++)? That definitely loops 19 times. Commented Jun 16, 2015 at 15:32
  • I put a System.out.println call immediately after the for, outside the while loop, and it prints for every value of i from 2 through 20. The while loops does no iterations for values of i other than 2. That could be what you are seeing. Commented Jun 16, 2015 at 15:34

3 Answers 3

4

The underlying problem has been identified in another answer. This answer is about avoiding the confusion that prevented the OP from seeing which loop was not executing, and therefore failing to find the bug.

When an inner loop is the entire body of an outer loop, it can be unclear which loop is not executing. Printouts and breakpoints that are inside the inner loop are useless for this purpose. The simplest solution is to add a statement at the start of the outer loop. If that statement executes multiple times, it is the inner loop that is not executing. The added statement could be almost anything, but a printout of a key variable for the loop iteration is particularly useful:

for (int i = 2; i < 21; i++) {
  System.out.println("for-loop, i=" + i);
  while (iIsFactor) {

Running the program with that added statement made it obvious that the outer loop was doing the full set of iterations, and the problem had to be with the inner loop.

Sign up to request clarification or add additional context in comments.

1 Comment

That's an incredibly helpful comment, thank you so much.
2

Your while and boolean declaration is not correct,

for(int i = 2; i < 21; i++){
    //reset flag for the start of each new run through the array
    iIsFactor = false;
    while(!iIsFactor){

2 Comments

That's right! Result after that change is "The smallest common multiple of the numbers from 1 to 20 is: 1" ideone.com/Fe1Ioz
@JorgeCasariego You wrote while(iIsFactor) instead of while(!iIsFactor).
2
while(iIsFactor){
        //reset flag for the start of each new run through the array
        iIsFactor = false;

After this, the next itteration of the loop, that while statement becomes false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.