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

PS1: where am I going wrong with the 1000th prime? This is what I have: cand=2 test=range(2,cand+1) count=1 while(count<100): if cand%test==0: cand=cand+1 else:count=count+1 if count==1000: print cand This is what I get: line5, in if cand%test==0: typeerror: unsupported operand type(s) for %: 'int' and 'list' huh?

OpenStudy (anonymous):

I fixed up your code a little bit so it should run. I tried to show you why you were getting that error message in the comments: http://pastebin.com/vngCLcHZ Some friendly suggestions: First, start by testing some smaller values (like maybe change your count parameter to 20, and see if you can get the first 20 primes successfully). You can do a lot of debugging this way. Second, you should think about a means of tracking. As you check your "cand" variable which "cand"s are composite and which are primes? You could probably use your test list (or tuple) for the primes, and maybe make a new tuple for the composites. Here is a sample of what I mean: http://pastebin.com/bX2cgZeF but I think you would be better off struggling through it a bit on your own too, so I didn't fix all your code. Third, don't give up...I adjusted your code so it works and you have the kernel of a very nice solution, just keep adjusting it till it works.

OpenStudy (anonymous):

Duh *facepalm*. Thank you very much for your help, I look forward to testing it out!

OpenStudy (anonymous):

Okay, this is what I have now. I dont' know what the ! means, but it was suggested to use in this fashion to denote what I want the program to do if the result is not 0. while(count<10): for num in range(2,cand): if cand%num==0: cand=cand+2 if cand%num!=0: count=count+1 cand=cand+2 print count,cand I think I want it to say while(count<10): for num in range(2,cand): if cand%num==0: cand=cand+2 else: count=count+1 cand=cand+2 print count,cand but when I enter this into IDLE, i get an error message regarding my use of else. i feel so lost. i'll get this at some point.

OpenStudy (anonymous):

please use a code pasting site: dpaste.com, pastebin.com, codepad.org, ideone.com you shouldn't change cand inside the loop without 'resetting' the loop or cand won't get tested with all the divisors - http://ideone.com/6bb9H

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!