Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 13 Online
OpenStudy (anonymous):

Can you find the problem in the code?

OpenStudy (anonymous):

I'm working on ps1a (compute the 1000th prime number) and when I run the code, I get no output/print. What am i doing wrong? The code is below: #Problem Set 1 #Name: Brian # Collaborators: None # Time: 4 hours # resultCounter = 0 primeCandidate = 1 divisor = 1 while resultCounter < 1000: while divisor <= primeCandidate * primeCandidate: if (primeCandidate / divisor) * divisor == primeCandidate: divisor = divisor + 1 else: divisor = divisor + 1 if divisor > sqrt(primeCandidate): resultCounter = resultCounter + 1 primeCandidate = primeCandidate + 2 divisor = 1 print primeCandidate #End of code. Thanks for taking a look guys!

OpenStudy (anonymous):

that last line divisor = 1 will reset the divisor each time, meaning that your while statement will never finish because the divisor will always be less than or equal to your primeCandidate. a good way to check for these things is to add a print statement; for example at the beginning of the block for the while statement you could add "print "testing with", divisor" and other things to let you see what the program is working with and what kinds of results it's getting midway through the process.

OpenStudy (maitre_kaio):

I don't agree with thinkpad when he says that the line divisor = 1 is at the wrong place. I agree when he says that you can check with print statements. Other advice, use a pastebin to show the code, it helps a lot when we want to read / test your code. That being said, I think the following line is wrong: if (primeCandidate / divisor) * divisor == primeCandidate When you enter the loop for the first time, it is evaluated as : if (1 / 1) * 1 == 1 which is always true. And then your program is stuck in an infinite loop.

OpenStudy (anonymous):

Thinkpad, now that you've mentioned it I do remember that trick, and I'll be sure to try it in the future! Thanks alot! And by the way I see what you meant by the divisor resetting to one every time, but the "while divisor" clause is dependent on "while resultCounter < 1000" if I structured it right. But I appreciate the point you're making! Maitre Kaio, I think I see what your saying but wouldn't the line, if (primeCandidate / divisor) * divisor == primeCandidate increase the divisor by one as a result, therefore avoiding that infinite loop scenario? As it turns out you're right; when I tried to shut down my computer the python shell was still chugging along at it! Anyway, I'm sure you're right about it but I'm trying to understand how. So if you could explain that bit, that would be great! Thank you very, very much guys! I'm doing this in conjunction with my normal high school classes, so I may or may not be able to work on it and check here tomorrow. But if I can then I'll be sure to try your ideas! Thanks again!

OpenStudy (maitre_kaio):

You are right, divisor is incremented -> divisor = 2. So the inner loop ends (because 2 > 1 * 1). Then divisor is reseted to 1, and the outer while loope does the job without ending. I encourage you to add some print statements in the main branches of your code to visualize it.

OpenStudy (anonymous):

Yes, you are correct that the divisor is incrementing. Sorry, I made a mistake when i copied the code and put an indent in the wrong place! Still quite the newb myself :D

OpenStudy (maitre_kaio):

Common mistake. That's why it's better to put the code in a pastebin

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!