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, as I've commented in the code, what I'm trying to achieve is a faster execution of my program by executing the 'while' loop only when iPrime = true. 50% of numbers are divisible by 2 and so this once this has been established the calculations can stop.
I'm doing this project as part of a beginners 'example' from a book, I actually am trying to calculate up to 1000000 as quickly as possible just for my own "extra credit"...
I read that the "short circuit 'and' operator" && only evaluates the second half of the statement if the first half is true, if it is false, the two are not evaluated against each other (saving CPU)
And it will also exit the loop, which will save even more CPU...
But for some reason, it is not working correctly! I've put more System.out.println() statements throughout, listing what 'iPrime' is - and the output is stranget... It switches iPrime on and off and evaluates every number, which I cannot understand.
The logical OR operator (||) will not work in a switch case as one might think, only the first argument will be considered at execution time.
Switch is similar to if else but there is a difference that we cannot use logical operators in switch statements , Because case requires constant expression as its value.
There is no such operator in switch statements. The switch statement operates on a single variable which is a value type or a string.
Using switch with conditions other than strict equality. If you wish, you can use comparison operators other than strict equality. The comparison expression used must evaluate to a Boolean value (therefore logical operators can be applied as well).
if((iPrime = true) && ...)
should be if((iPrime) && ...)
.
By doing isPrime = true
then you are assigning true to isPrime
and not comparing its' value to true
.
You might also want to see this to better understand what happens in your code:
At run time, the result of the assignment expression is the value of the variable after the assignment has occurred. The result of an assignment expression is not itself a variable.
So, when you use the =
operator instead of ==
(Which is removed when you compare something to true
- instead of writing if(someBoolean == true)
you just write if(someBoolean)
), you're actually satisfying the condition, always!
Simply change =
into ==
, that is, change
while ((iPrime = true) && (j < (i / 2 + 1)))
into
while ((iPrime == true) && (j < (i / 2 + 1)))
A 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 method I can think of is Sieve of Eratosthenes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With