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

Can anyone offer advice for my code in ps1? The program runs but times out without a result. I'm pretty sure the problem is in the loop where I divide the prime candidate by all the primes less than its square root. But I have no idea how to fix that. http://codepad.org/4lgfO1cd

OpenStudy (anonymous):

You've got an infinite loop, line 8: while number < highdivisor

OpenStudy (anonymous):

Make sure you change either 'number' or 'highdivisor' so that you eventually break out of the loop

OpenStudy (anonymous):

Ugh- so obvious and yet it escaped me. Thanks!

OpenStudy (tonybaldwin):

But with only [2, 3] in her list, shouldn't the loop only run twice with "for number in confirmedprimes:"? Admittedly, my python fu sucks, but if I did this in bash for number in [ 2, 3 ]; do something done it would loop only twice.

OpenStudy (anonymous):

True. However, the script will add more elements to the list as it runs (but I would prefer using confirmedprimes.append() since it would add it to the end of the list instead of inserting the new element right before the end of the list). The for loop runs inside a while loop, so once the code loops the for loop will use a longer confirmedprimes list.

OpenStudy (tonybaldwin):

I guess I'll have to wait until I get to that assignment to figure that out, because I'm confused. Then again, I have a BA in English, and am no mathematician.

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!