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

hi! i'm stuck with assignment 1, 1st problem. here's my code which i cannot get to work correctly -- http://codepad.org/2yrVKqVK

OpenStudy (anonymous):

Try solving the problem first to one prime, rather than trying to accepting input from the user. Look below at s-kmarti19's questions regarding pset 1, I tried to explain a lot of the concepts, and minor details. Just avoid clicking on the codepad links :-)

OpenStudy (anonymous):

thanks, here's the next version -- http://codepad.org/wjnnRMdg but it won't work either, it says "TypeError: can only concatenate tuple (not "int") to tuple" i remember in the 3rd lecture there was smth like "divisors += (x)", can't see how my case is different from that.

OpenStudy (anonymous):

even if I solve this problem by adding "prime = numToTest,", thus turning the integer into a tuple, there's still another error: IndexError: tuple index out of range for some reason those indexes just don't count correctly.

OpenStudy (anonymous):

found a missing line in the previous link, here's the "correct" version -- http://codepad.org/E12FsGLr

OpenStudy (anonymous):

Things I noticed: You didn't really understand how to translate the check for primality into code. Try to write what you think your code should look like, try making it work for one prime first etc. Like, you are actually checking for primes between [0,999), that's not what the assignment is about. Another point is that you are writing a == 3, instead of a = 3. a == 3 will just return True, which is not what you want. Other thing is: think about using lists rather than tuples. I think lists are faster and more intuititive. Also, you don't need to check if a%i != 0, only if it is == 0.

OpenStudy (anonymous):

Nevermind the point about the range, I think I misunderstood your code at first glance. :-)

OpenStudy (anonymous):

http://codepad.org/X018omzZ this one looks better, but still doesn't work right...

OpenStudy (anonymous):

where did you find your primality test? and did you implement it correctly? The test that you have implemented is not working. A quick spot check of the number 221 (which is the product of two primes 13 x 17) shows this. 221 is not prime but your test says that it is. what you have attempted is an optimization - if your method worked it would execute faster but not better. I think that you are on the right track and if you used the test given in the pset then you could make your code work hmm, it looks like your for loop may not be doing what you think - it will 'process' one-thousand loops - it will test 1000 numbers, it will not find 1000 primes it might help if you declared your list of primes outside of the function so that it could be inspected after the function has completed.

OpenStudy (anonymous):

it's written in wikipedia: "all primes are of the form 6k ± 1, with 2 and 3 being the only exceptions", so that's what the previous test was based on. http://codepad.org/Ju8NdmQM -- this thing gets the first 20 answers right, however still fails when it comes to the 1000th prime.

OpenStudy (anonymous):

http://codepad.org/KODYn7l5 -- that's it! thank you, guys, for help :)

OpenStudy (preetha):

silli, did this conversation on OpenStudy help you in what you were trying to learn from MIT OCW?

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!