Hi, I have been trying ps1 for quite some time now. I thought i would try to work on the first one on my own but its harder than it looks. I have already tried several versions , i have decided to go with this. But i still have the problem of unending loop. can someone take a look?? THANKS A LOT! http://pastebin.com/E0bCMRQa
Your variable scope is behaving weirdly. To use while loops, you have to have a break case, that is, a case that will force the program out of the loop. While you do have its skeleton (while < 1000 and while num > div), they are not doing what you think they are doing. Search for local and global scope (a simple solution to this problem is to make isPrime having only two arguments and use only one variable(primeCount) as global. This will cause the loop to exit, albeit is far from elegant or stuff like that). With all that being said, your program is not doing what you are thinking it's doing. It's bugged. The tests inside isPrime should be improved. As a last note, try avoiding using that many variables with the same name, sometimes it gets a bit confusing, especially in Python that does not have support for data hiding, nor have that many local/global differences (speaking syntactically). At least for me, it was a bit confusing.
As a general tip, try to make a code that checks whether or not a given number is a prime first, then build on top of this code. Don't try to tackle the big problem and generate all 1000 prime numbers at once.
Thanks bmp! I guess I will create a function that tests if a number is prime or not first then add to that. What do you mean by make primeCount as global? Shouldn't I change it everytime I find a prime?
Yup, but to do that, your variable (as you are implementing it) should have global scope. What I meant is this: http://codepad.org/fT2gS1Xf . Read the Python docs about variable scope. As a matter of fact, you can implement a solution without using global variables (they are, sometimes, seen as a bit of a kludge if they can be avoided), but the important thing is that you can implement a working solution first, then look for improvements / cleaner code.
Thanks for the input. I could atleast make a program that tests for prime. Now the big hurdle is the real assignment. Here's what I got. I think I will have trouble expanding it because my break for while is very awkward. Any input is appreciated! http://codepad.org/rdnX3wLr
appears to work well. However, it will be more useful if you are able to put the while loop inside the function... so that you can simply send a number into the function to get an answer. I would also want to see a True or False answer. That will be easier to expand.
Looks good, but there are some clear improvements: you are checking pair numbers to see if they are prime, not really good. Also, you are checking for divisors up to a given number. How soon can you stop? That will drastically improve your running time. Now that you have a working solution, think about adding some comments, also. Specially docstrings. To implement a program to find 1000th prime, what do you need? You need a loop and a count/list, so you can check your progress and a way to break out of your loop. If you are stumped, post again what is your problem, and I will try to help you out :-)
For instance, instead of printing "Is prime" your function could return True, and if returned true you could increase the count by 1, and then break if the count == 1000, or some input. That's a possibility.
Join our real-time social learning platform and learn together with your friends!