Using logic inside a switch statement

My question is related to the following code:

public static void main(String[] args) { // Find Prime Numbers from 0 to 100 int i; for (i=2; i <= 100; i++) { int j = 2; boolean iPrime = true; //The following line gives incorrect results, but should execute faster // while ((iPrime = true) && (j < (i / 2 + 1))) { //The following line gives correct results but performs un-necessary operations //by continuing to calculate after the number is found to be "not prime" while (j < (i / 2 + 1)) { j++; if ((i % j) == 0) { iPrime = false; //System.out.println(j + " is a factor of " + i); } } if (iPrime) { System.out.println(i + " is a prime number!"); } } } 

Now that I have commented on the code, I am trying to get my program to run faster by executing the while loop only when iPrime = true. 50% of the numbers are divisible by 2, and therefore, after this is established, the calculations may stop.

I am doing this project as part of a beginner’s example from a book, I’m actually trying to calculate up to 1,000,000 as quickly as possible just for my own β€œextra credit" ...

I read that the "short circuit" and the "operator" && only evaluate the second half of the operator, if the first half is true, if it is false, the two are not evaluated against each other (CPU saving)

And it will also exit the cycle, which will save even more CPU ...

But for some reason it is not working properly! I put in more system instructions to System.out.println (), indicating what "iPrime" is, and stranget is displayed ... It turns iPrime on and off and evaluates every number that I cannot understand.

+6
source share
4 answers

if((iPrime = true) && ...) must be if((iPrime) && ...) .

By doing isPrime = true , you assign true isPrime , rather than comparing its value with true .

You can also see this one to better understand what is going on in your code:

At run time, the result of the assignment expression is the value of the variable after the assignment. The result of the assignment expression is not the variable itself.

So, when you use the = operator instead of == (which is deleted when comparing something with true - instead of writing if(someBoolean == true) you simply write if(someBoolean) ), you really always satisfy the condition!

+7
source

Just change = to == , i.e. change

 while ((iPrime = true) && (j < (i / 2 + 1))) 

in

 while ((iPrime == true) && (j < (i / 2 + 1))) 

Full code with better performance

 public static void main(String[] args) { // Find Prime Numbers from 0 to 100 System.out.println(2 + " is a prime number!"); for (int i = 3; i <= 100; i += 2) { boolean isPrime = true; for (int j = 3; j * j <= i; j += 2) { if ((i % j) == 0) { isPrime = false; break; } } if (isPrime) { System.out.println(i + " is a prime number!"); } } } 

The fastest way I can think of is through the Sieve of Eratosthenes .

+4
source

johnchen902 / Marun Marun fixed the error; in addition, the optimization you can perform is -

 System.out.println("2 is a prime number!"); // the for loop won't detect 2 anymore for (i=3; i <= 100; i+=2) { 

In addition, instead of executing the modulo operator for a number with all previous odd numbers, to see if it is prime, you can execute the modulo operator for a number with all previous strokes to see, for example, save the prime numbers that you will find in ArrayList, and iterate through the list in your primary test.

And for a very efficient (in terms of processor efficiency (but less area-efficient) algorithm, use the Sieve of Eratosthenes

+1
source

First, when I ran your code, I found that it shows 4 as a prime, which is incorrect. The reason is the location of your j ++ string. You should modify the while loop as follows:

 while (j < (i / 2 + 1)) { if ((i % j) == 0) { iPrime = false; //System.out.println(j + " is a factor of " + i); break; } j++; } 

Moving on to the second part, you want to avoid additional calculations when you establish that the number is not prime. You can use the break statement to do this, as in the code above.

The commented part does not work because you have an assignment instead of comparing equality.

+1
source

All Articles