Primes and boolean logic

363 views Asked by At

I wanted to write a program to find all the primes from b to a so I wrote this code (which worked):

public static void main(String[] args) {    
    int a;
    int c;
    boolean isPrime;

    a = 2;
    c = 0;

    while(a <= 100000 ){
        isPrime = true;
        for (int b = 2;b<a; b++){
            c = a%b ; 
            //System.out.println(c);

            if ( c == 0){
                // this stores every not prime number
                isPrime = false;                            
            }                           
        }

        if (isPrime){
            System.out.println(a);
        }           
        a=a+1;  
    }                                   
} // main end

Next I tried to write a variation of this code, which did not work:

public static void main(String[] args) {
    int q;
    int w;
    boolean isPrimeOne = false;

    q = 2;
    w = 0;

    while(q <= 100){
        isPrimeOne = false;
        for (int d = 2; d<q; d++){                      
            w = q%d;
            if( w != 0 ){
                isPrimeOne = true;
            }                                               
        }   
    }

    if(isPrimeOne){
        System.out.println(w);
    }

    w = w+1;
}

It seems to me like my first and second codes are very similar. The only difference (I see) is that I initialized isPrime as true in the first one, and as false in the second one.

Why does the first code work and the second does not?

3

There are 3 answers

1
rgettman On BEST ANSWER

The 2 code examples are similar, but they are opposite in the initializations and handling of the isPrime and isPrimeOne variables.

The first code assumes that a number is prime (true) until a factor is found. That proves the number is composite, so it's set to false and it stays that way until the loop finishes. Just one factor is needed to prove that it's composite.

However, the second code assumes that a number is composite (false) until a number is found that isn't a factor. But just because a number was found not to be a factor doesn't mean that it's prime. E.g. 20 doesn't have 3 as a factor, but it's still composite. You are printing w, the remainder, instead of q, the number you're testing. You are incrementing w instead of q, and that is outside the while loop instead of inside the while loop.

0
Didier L On

Four problems here:

  • you are incrementing w instead of q;
  • you are incrementing it outside the while loop (check you matching curly braces);
  • you have inverted the logic for isPrimeOne with respect to isPrime in every assignment but not in the check for printing the number, so you are outputting something only for non-prime numbers;
  • you are outputting w instead of q.

I suggest that you reduce the scope of your variables as much as possible, there is no need to declare all variables at the beginning of the method. If you declare w and isPrimeOne inside the while loop, this will prevent you from trying to use them elsewhere by mistake.

0
Balkrishna Rawool On

"It seems to me like my first and second code are very similar. the only difference (I see) is that I initialized is prime as true first, and is prime as false second."

Well actually not. There are more differences in those two programs. Just do a simple compare of each line and you'll find them out.

"why does the first code work and the second code not work"

Because the first program follows a valid algorithm for finding out prime numbers. The second program does not follow any such algorithm. For details see rgettman's answer.