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

During lecture 3, 2 methods were shown for finding whether a number is a perfect cube or not. If I implement using WHILE loop, it works perfectly for integer 1957816251 but shows memory error on using FOR loop. Why and how can I rectify this?

OpenStudy (espex):

Post your for loop.

OpenStudy (anonymous):

x=int(raw_input("Enter an integer ")) for ans in range(0, abs(x)+1): if ans**3 == abs(x): break if ans**3 != abs(x): print str(x)+' is not perfect cube' else: if x < 0: ans=-ans print 'The Cube root of '+str(x)+' is '+str(ans)

OpenStudy (espex):

It is producing a MemoryError because you are iterating 1957816252 times.

OpenStudy (anonymous):

it is Probably not because you are iterating that many times but because you are creating a list that is that big. try xrange instead of range: http://dpaste.com/1217969/ http://docs.python.org/2.7/library/functions.html

OpenStudy (anonymous):

Thanks alot bwCA....xrange worked.

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!