Why does 1**2**3**4**5 raise a MemoryError?

Dave Angel davea at davea.name
Sun Mar 31 03:34:31 EDT 2013


On 03/31/2013 02:56 AM, morphex wrote:
> Hi.
>
> I was just doodling around with the python interpreter today, and here is the dump from the terminal:
>
> morphex at 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.
>
>

Perhaps you didn't realize that the expression will be done from right 
to left.  So first it calculates 4**5 which is 1024

Then it calculates 3**1024, which has 488 digits.  then it calculates 2 
to that power, which is a number large enough to boggle the mind.  That 
number's storage needs makes a few gigabytes seem like a molecule in the 
ocean.

Anyway, it never gets around to doing the 1**  part.

On the other hand, perhaps you wanted to do a different calculation:

 >>> ((((1**2)**3)**4)**5)
1






-- 
DaveA



More information about the Python-list mailing list