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?
Post your for loop.
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)
It is producing a MemoryError because you are iterating 1957816252 times.
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
Thanks alot bwCA....xrange worked.
Join our real-time social learning platform and learn together with your friends!