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

Hoping I can get an answer to this. For ps1 I wrote this code: count = 0 n = 1 while count < 1000: n = n + 1 divisors = 0 for testdiv in range(1,n): testrem = n%testdiv if testrem == 0: divisors = divisors + 1 if divisors < 3: count = count + 1 print ("the thousandth prime number is", n) I know this is not great style (doesn't even limit test to odd numbers) but I was hoping to get it to work. When I run it I get 7699, which is the 977th prime number, not the 1000th. Not sure why this is. If anyone has an inkling please reply.

OpenStudy (anonymous):

Run this program and see if it helps by reading the print lines in the shell... It's only up to 30 though. http://dpaste.com/563124/

OpenStudy (anonymous):

http://primes.utm.edu/lists/small/1000.txt a list of primes.

OpenStudy (anonymous):

This is what I came up with. I'm not sure right now what you did. http://dpaste.com/560941/

OpenStudy (anonymous):

well i just figured it out.. you just need to replace the range(1,n) by range(1,n+1) you intend to include n in the calculation..so you need to set the upper range to n+1 otherwise it will not include n Another way to rectify is to replace 3 in " if divisors < 3:" by 2 because your code does not include the number itself while counting devisors. i have uploaded the 2 rectified versions of your code. and both of the return the correct value 7919 http://pastebin.com/nWjPbyx1 http://pastebin.com/rWmZQfBX

OpenStudy (anonymous):

sunu0000 .... Thanks! To quote Ren: "Stimpy, you're a genius!" I didn't realize you had to go one up from the upper limit on the range statement. I see now that my code above was returning "primes" when there was one other divisor besides 1 and the number itself (i.e.squares like 9 and 25). No wonder I came up just a bit short of the 1000th. Thanks again!!!

OpenStudy (anonymous):

Glad i could help :D

OpenStudy (anonymous):

Sunu, I understand the code, but something I am just not getting. Why does the divisors have to be less than 2. Wouldn't it always be equal to 2 if it's a prime considering it is dividing by 1 and then dividing by the number it is testing to get a remainder of 0? The code works, but I just don't understand, I feel like it should be if divisors == 2. Is there something I am missing? Thanks

OpenStudy (anonymous):

well u r absolutely right...but your observation of the code was slightly wrong i think.. take another look at the code.. u can see the when we set the condition to divisors < 3 ..it includes the number itself.. but when the condition is divisors < 2...divisors do not include the number we are testing..so the only divisor is 1

OpenStudy (anonymous):

Yes, I realized it like 5 minutes after I wrote that. I could have sworn that I posted a follow up stating I understood why you wrote it that way. Thanks.

OpenStudy (anonymous):

LoL....i thought only i have to to post multiple times to make it shoe up here..

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!