way to calculate 2**1000 without expanding it?

Grant Edwards invalid at invalid.invalid
Sat Sep 17 12:42:18 EDT 2011

On 2011-09-17, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Fri, Sep 16, 2011 at 8:19 PM, Grant Edwards <invalid at invalid.invalid> wrote:
>>> Here's another one-liner using a generator instead of map:
>>> ? ? ?sum(int(c) for c in str(2**1000))
>> Just in case you can't spare the 3KB required for the list of integers
>> that map creates. :)
>> In this case it doesn't matter, but it's not hard to find problems
>> where the difference between the memory requirements for a generator
>> and a map/list-comprehension are significant enough to worry about.
> Unless otherwise specified, I generally assume that code on this list
> is for Python 3, and map in Python 3 returns an iterator.

Indeed, I forgot about that (I'm still using Python 2 since I use some
libraries that haven't been ported).


More information about the Python-list mailing list