Home > other >  Python history of small white wall (circulation and assignment)
Python history of small white wall (circulation and assignment)

Time:10-13

2019/11/17
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 prescribing

CodePudding user response:

reference 1st floor JMZL response:
end position directly prescribing line

What do you mean    how to ten words

CodePudding user response:

refer to the second floor weixin_45843097 response:
Quote: refer to 1st floor JMZL response:
end position directly prescribing line

What do you mean    how to ten words

Not one by one to test whether divisible, in addition to the square root of the integer plus one

CodePudding user response:

reference JMZL reply: 3/f
Quote: refer to the second floor weixin_45843097 response:
Quote: refer to 1st floor JMZL response:
end position directly prescribing line

What do you mean    how to ten words

Not one by one to test whether divisible, in addition to the square root of integer have to do is add a

As number theory knowledge, have not learned

CodePudding user response:

Are usually advised to determine whether a prime packaged into a function, so it is isolated, it is not easy to confusion
  • Related