1
public class Q3
{
public static void main(String args[]){
int i, j;
int Max = 1000;
    //It's obvious that the first fifty prime numbers are less than 1000.
int counter = 1;
while (counter <= 50){
    for (i = 2; i < Max; i++){
        for (j = 2; j < i; j++){
            if ( i % j == 0){
            break;
            }
        }
        if (j >= i){
            System.out.printf("%s ", i);
            counter++;
        }
        if(counter % 10 == 0){
        System.out.print("\n");
        }       
    }
}

}
}

This is a program that I wrote to list first 50 prime numbers, ten of them per line. However, it's not working properly because of the while loop. After execution, this program lists all prime numbers less than 1000. It seems that the while loop is not functioning at all. Can anyone tell me the reason? Many thanks.

4
  • 1
    how will j be greater than/equal to i ? Commented Sep 25, 2013 at 8:18
  • Just for the record. There shouldn't be a while loop at all. Only use while loops when it is not sure that the loop will be executed at all. In your case you will definitely enter the loop. In this case, use do-while. Commented Sep 25, 2013 at 8:28
  • Many thanks guys. But I may need to rephrase my question.(Sorry for my English, it's not my mother tongue.) I need that while loop to keep track of the prime numbers I've listed. The algorithm is that I check every integer one by one. The value of counter is only incremented when the number checked is primitive. Unfortunately, the code doesn't work that way. Again, many thanks Commented Sep 25, 2013 at 8:36
  • @user2814227 why don't you try the solutions without the while? They do just that. Commented Sep 25, 2013 at 8:47

3 Answers 3

1

The primes are generated by the first for loop. The while body is only executed once.

You could remove the while and instead use a different condition on the for:

for (i = 2; counter <= 50; i++){
Sign up to request clarification or add additional context in comments.

Comments

0

you have a big problem, the true code is following:

int i, j;
int Max = 1000;
//It's obvious that the first fifty prime numbers are less than 1000.
int counter = 0;
for (i = 2; i < Max && counter < 50; i++){
    for (j = 2; j < i; j++){
        if ( i % j == 0){
        break;
        }
    }
    if (j >= i){
        printf("%d ", i);
        counter++;
         if(counter % 10 == 0){
             printf("\n");
         }  
    }    
}

the output is: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229

2 Comments

Thanks a lot. It works, and this is what I want. Could you plz explain to me what the big problem is and the reason why while loop is not working. BTW, I'm also Chinese.(Judging from ur surname.)
the first problem is that only when "for (i = 2; i < Max; i++)" ends, "while (counter <= 50)" just starts judging if counter<=50; the second one is "if(counter % 10 == 0){ System.out.print("\n"); } " if counter first adds to 10,print a new space line, if the next N numbers are not prime so counter is still 10, so the statement also satisfies, it will print N space lines. the third one is that if counter initializes 1, there will be 9 not 10. My English is not great, so I donnot know if you understand me.
0

Why are you not writing a boolean isPrime(int number) function? You will have to check if it is true, and if it is, increase the counter and print the number. Here is a naive implementation, I have seen some other better implementations:

boolean isPrime(int number) {
  if (number < 2 || number % 2 == 0) {
    return true;
  }
  double sqrt = Math.sqrt(number);
  for (int i = 3; i <= sqrt; i += 2) {
    if (number % i == 0) {
      return true;
    }
  }
  return false;
}

Inside your for:

for (i = 2; i < max; i++) {
  if (isPrime(i)) {
    counter++;
    // print number, print new line if needed
  }
}

3 Comments

yup this is an elegant way of doing it. But i only wanted to get things done easily. Many thanks, anyway.
Elegant way is usually the way of doing things easily.
agreed, will try later

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.