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

I have a question regarding problem set 2 (problems 3 and 4). The code I wrote for question 3 (codepad.org/0kL2qT3X) gives me the correct answer. However, when I try to adapt the code (codepad.org/VCopk3Ib) in order to solve question 4, I get a 'Timeout'. Can anyone tell me why? Thanks

OpenStudy (anonymous):

sometimes it helps to put print statements in your code to see what is going on. http://codepad.org/h2ig9GJy looks like you are on the right track - you might take a look at the limits in your range functions

OpenStudy (anonymous):

Thanks for your help. Using multiple break statements did the trick. Is there any way to break out of nested loops more elegantly?

OpenStudy (anonymous):

break is the only way i know of to break out of a loop After each for statement i 'broke out' if the the equation evaluated to greater than n. i thought that if you limited the range of your for loops you might not run into the timeouts. you weren't getting timeouts on your computer were you?

OpenStudy (anonymous):

here is a solution using a function http://dpaste.com/723412/

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!