On Sat, 30 Mar 2013 23:56:46 -0700, morphex wrote: > Hi. > > I was just doodling around with the python interpreter today, and here > is the dump from the terminal: > > morphex@laptop:~$ python > Python 2.7.3 (default, Sep 26 2012, 21:53:58) [GCC 4.7.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> 1**2 > 1 >>>> 1**2**3 > 1 >>>> 1**2**3**4 > 1L >>>> 1**2**3**4**5 > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > MemoryError >>>> >>>> > Does anyone know why this raises a MemoryError? Doesn't make sense to > me.
Because exponentiation is right-associative, not left. 1**2**3**4**5 is calculated like this: 1**2**3**4**5 => 1**2**3**1024 => 1**2**373...481 # 489-digit number => 1**(something absolutely humongous) => 1 except of course you get a MemoryError in calculating the intermediate values. In other words, unlike you or me, Python is not smart enough to realise that 1**(...) is automatically 1, it tries to calculate the humongous intermediate result, and that's what fails. For what it's worth, that last intermediate result (two to the power of the 489-digit number) has approximately a billion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion digits. (American billion and trillion, 10**9 and 10**12 respectively.) -- Steven -- http://mail.python.org/mailman/listinfo/python-list