way to calculate 2**1000 without expanding it?

Stefan Krah stefan-usenet at bytereef.org
Fri Sep 16 23:07:31 CEST 2011


Gary Herron <gherron at digipen.edu> wrote:
>>>> i am writing a program to sum up the digits of a number 2**1000?
>>>> Is there a way/formula to do it without expanding it?
>
> Here's another one-liner using a generator instead of map:
>
>     sum(int(c) for c in str(2**1000))

The OP did not specify the base:

>>> bin(2**1000).count('1')
1


Or just:

>>> print(1)
1


Stefan Krah





More information about the Python-list mailing list