Well this is my first attempt at the first assignment and I can't really see what's wrong with my program http://dpaste.com/570759/ (I know I kind of cheated by starting at 3, but I don't understand what I did wrong
try putting if i==1000:break inside the for loop.. take a close look...u can see your code never exits the for loop ...so u need a break statement for that :)
I wouldn't call starting at 3 "cheating", its the first simple step, i know when i was debugging code i started at 7 looking for 11 and skipping 9, what ever works is the right answer. keep in mind there is probably a better answer, but by the time its important, you'll know how to handle it
This is funny. Your code SHOULD have stayed in the for loop, since after a certain value of x, then x would just keep on increasing and increasing. Yet it doesn't. I'm gonna look into this a bit more.
Okay try to run my program. It is the same, with print statements added. Let it run to about 40 or so then press control c to stop it. You will see that it works okay in the first 30 but then there is problems. Essentially the problem is that it only checks one number for a divisor before it deems it prime or not. http://dpaste.com/571072/ for example check 35.. "35 is a prime as 35 % 3 !=0" unless I'm reading it all wrong.
Okay, so I think I've figured out why your for loop behaves so erratically. In Python's documentation, it says: "Note There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, i.e. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop."
This is the only possible cause that I can find.
so the internal counter reaches the end of the sequence when x is exactly double of a.. that will mean the upper range is actually treated as x/2=a and due to the fact that a doesn't lie in the range(2,a) ..the for loop ends...that is it?
More or less like that, yeah. Just a guess though.
seems likely to me -_-
thanks for all the help, really appreciate it
Join our real-time social learning platform and learn together with your friends!