Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

java - What is the role of temp = 0 in finding primes?

I have the following code to print all prime numbers from 2 to 100:

int number1 = 2, number2 = 100, temp = 0;
System.out.println("prime numbers between" + number1 + "and" + number2 + "are :");

for (int i = number1; i <= number2; i++) {
    for (int j = 2; j <= i / 2; j++) {
        if (i % j == 0) {
            temp = 1;
            break;
        } else {
            temp = 0;
        }
    }

    if (temp == 0) {
        System.out.println(i);
    }
}

What is the role of temp = 0 in the very beginning?

If I modify it to, lets say 1, I get a different output. The code then prints all primes starting from 5 instead of 2. Why are the other numbers skipped?

question from:https://stackoverflow.com/questions/66049342/what-is-the-role-of-temp-0-in-finding-primes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Explanation

Have a close look at your loop logic flow:

for (int i = number1; i <= number2; i++) { 
    for (int j = 2; j <= i / 2; j++) {
        if (i % j == 0) {
            temp = 1;
            break;
        } else {
            temp=0;
        }
    }

    if (temp == 0) {
        System.out.println(i);
    }
}

i starts as 2 and increments. The inner loop is:

for (int j = 2; j <= i / 2; j++) {

Inner loop is skipped

That means that for the first iterations of the outer loop, for example i = 2 the condition of the inner loop evaluates to:

j <= i / 2
// which is
2 <= 2 / 2
// which is
2 <= 1

Hence the inner loop does not even enter at all and is skipped. So we directly reach

if (temp == 0) {
    System.out.println(i);
}

The same is true for the iterations, i = 3.

i = 4 is the first iteration that actually enters the inner loop and starts overwriting temp with either 0 or 1.


Meaning of temp == 0

So during the first iterations of the outer loop (i = 2, i = 3), the initial state of temp plays a role, since it determines whether i will be printed or not.

So you need it to start as 0 to have the first values, for which the inner loop is not even entered, included in the output.


Notes

That said, using temp in such a way is overly complicated. It would be better if it would be moved to the place where it is actually needed, inside the loop, given a better name and also changed to a boolean. All in all, you may simplify the code as follows:

int min = 2;
int max = 100;
System.out.println("prime numbers between" + min + "and" + max + "are :");

for (int i = min; i <= max; i++) {
    boolean isPrime = true;
    for (int j = 2; j <= i / 2; j++) {
        if (i % j == 0) {
            isPrime = false;
            break;
        }
    }
    if (isPrime) {
        System.out.println(i);
    }
}

And if you are willing to introduce a helper method like:

public static boolean isPrime(int candidate) {
    for (int i = 2; i <= candidate / 2; i++) {
        if (candidate % i == 0) {
            return false;
        }
    }

    return true;
}

Your code will heavily simplify and be very easy to read:

int min = 2;
int max = 100;
System.out.println("prime numbers between" + min + "and" + max + "are :");

for (int i = min; i <= max; i++) {
    if (isPrime(i)) {
        System.out.println(i);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...