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

I'm working on problem set 1, problem 1. I think I'm close, but it's giving me a prime number that is too low to be the 1000th. Here is my code, could anyone tell me where I went wrong? primecounter = 1 # how many primes found, start 1 to include 2 primetester = int(3) # test if prime while primecounter < 1000: for x in range (2, primetester): if primetester%x == 0: primetester = primetester + 2 #not a prime else: primecounter = primecounter + 1 #is a prime primetester = primetester + 2 primetester = primetester - 2 #undo the last addition print primete

OpenStudy (anonymous):

You can't increment primetester inside your loop that looks for a factor. If you print the values of x, and primetester inside your for loop but before your if you'll see the issue.

OpenStudy (anonymous):

I tried printing those like you said, but I'm a little confused by what comes up, as to which values are x and which are primetester, and why they are what they are. Am I misunderstanding the for loop? What I'm trying to do is for it to take primetester, and divide it by all numbers between two and itself, and it if gets 0 as a remainder for anything, add two to primetester. If not, add one to primecounter and two to primetester, until primecounter is 1000. I guess my questions is, why can't I increment primetester where I did? I don't totally understand the syntax of for loops, so that may be my problem. Also, is my idea of using a for loop a possible way to do it in thte first place, or should I start over and try another method?

OpenStudy (anonymous):

To clarify which is which use the statement print 'X is', x, 'Primetester is', primetester It's not a syntax problem. It's more a matter of algorithm flow. Let's walk through the code by hand for a moment.. primecounter is 1 primetester is 3 (you don't need int() here, 3 is an int) is primecounter < 1000 ? yes -> x is 2 is primetester(3)%x(2) == 0? no -> primecounter is 2 primetester is 4 x is 3 is primetester(4)%x(3) == 0? no -> primecounter is 3 primetester is 5 ..... Clearly we have some problems here. You cannot be sure that a number is prime until you've exhausted all the possible divisors. You also need to restart the divisor loop back to 2 each time you choose a new number to test for 'primeness'. If you find you're getting hung up on syntax try just writing the process flow in English and we can debug it independently of the code.

OpenStudy (anonymous):

sorry, I didn't see that you were incrementing primetester by 2, so the problem probably won't show up until you get to 9 or so.

OpenStudy (anonymous):

Thank you so much, I'm finally starting to understand this! I modified my code a bit, and while it's closer I think, it's still incrementing primecounter every time a number has a remainder. primecounter = 1 # how many primes found, start 1 to include 2 primetester = int(3) # test if prime while primecounter < 1000: for x in range (2, primetester): print 'x is', x print 'primetester is', primetester if primetester%x != 0: primecounter = primecounter + 1 #is a prime primetester = primetester + 2 primetester = primetester - 2 #undo the last addition print primetester I'm pretty sure I understand where I'm going wrong, but I'm not sure how to fix it. Is it possible to continue with what I've got and somehow only add to primecounter once it has tried ALL the xs for a certain primetester? Or is a for loop just not the best choice for this problem?

OpenStudy (anonymous):

I got it! I was just thinking about it, and I realized I could do it if I just added another variable. For anyone who's curious, this is how I did it. primecounter = 1 # how many primes found, start 1 to include 2 primetester = int(3) # test if prime notprime = 0 while primecounter < 1000: for x in range (2, primetester): if primetester%x == 0: notprime = 1 if notprime == 0: primecounter = primecounter + 1 #is prime primetester = primetester + 2 else: primetester = primetester + 2 notprime = 0 #reset primetester = primetester - 2 #undo the last addition print primetester

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!