Today finally straightened out the prime judgment
The first code is mainly judgment for many times, probably like this (no s, initially ignored here) :
And the result will be like this:
The main cause of this result is (5, for example)
When I take 5, 2 of j, judgment is not a prime number and output
Also, take 3, 4 j judgement is not and output 5 is a prime number
So finally got three output
I try to change their, distinguish the prime number can be directly output and stop the inner circulation, no problem, but when the judgment cannot be divided exactly by a certain number, and can't judge it is a prime number, but to all j not divisible I can confirm that it is a prime number
So I set up a S to record cannot be divided exactly by j I, if all j not divisible I will output decide its are prime Numbers, and as long as there is a divided exactly by j I will output the I is not a prime number, and break,
After the change:
This seem feasible, as a matter of fact, such results:
9 was convicted for two times, and the different results, through after the group communication, bigwig, I have a break problem
I=7, j 2, judge not divisible, execute the assignment operation s=1, then the judge did not change the value of s
I=8, 2 of j is direct output is not a prime number, break
When I=9, j take 3 when the output is not a prime, but at the same time to terminate the inner loop, the back and to determine if statements, s=1, at this time so will output 9 is a prime,
Modify the following
Initialized each inner circle, just change the position of the s=0, but the effect is very different to
CodePudding user response:
The end position directly prescribingCodePudding user response: