way to calculate 2**1000 without expanding it?

Z

zombie

Hi guys,
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?
 
A

Arnaud Delobelle

Hi guys,
        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?

In base 2, the answer is 1 :)

Hopefully-helpfully-but-not-so-sure'ly yours,
 
I

Ian Kelly

Hi guys,
        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?

Possibly, but why worry about it? It's only around 300 digits.

Since this sounds like homework, I won't post the one-liner I used to
do it the brute-force way, but I will note that it takes about 200
microseconds to run on my laptop.

Cheers,
Ian
 
P

Prasad, Ramit

Hi guys,
Since this sounds like homework, I won't post the one-liner I used to
do it the brute-force way, but I will note that it takes about 200
microseconds to run on my laptop.

If you happen to be new to Python, I will say I used these functions to create a one liner (in alphabetical order).

Int
Lambda
Map
Reduce
Str


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This emailis confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
 
A

Arnaud Delobelle

Possibly, but why worry about it?  It's only around 300 digits.

Since this sounds like homework, I won't post the one-liner I used to
do it the brute-force way, but I will note that it takes about 200
microseconds to run on my laptop.

Ah go on, let's make a codegolf contest out of it.
My entry:
1366
 
G

Gary Herron

Ah go on, let's make a codegolf contest out of it.
My entry:

1366

Here's another one-liner using a generator instead of map:

sum(int(c) for c in str(2**1000))

Gary Herron
 
S

Stefan Krah

Gary Herron said:
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:
1


Or just:
1


Stefan Krah
 
P

Prasad, Ramit

sum(map(int,str(2**1000)))
*smacks face* Gah, forgot about sum.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
 
S

Steven D'Aprano

Arnaud said:
Ah go on, let's make a codegolf contest out of it.
My entry:

1366


Time for some obfuscated Python!

b = (16,13624)
for i in range(23, 42, 3):
b = (b[0]*b[0], b[1]+1024)

for j in range(12345, 8929, -7):
b = (b[0]+b[0], b[1]-3)

while b > (0,9876):
b = tuple((lambda a,b: map(lambda a,b:a+b, a,b))
((lambda a,b: map(lambda a,b:a*b, a,b))
(b,(0,1)), (lambda a,b: map(lambda a,b:a-b, a,b))
(divmod(b[0],10),(0,64))))

print b[1]
 
G

Grant Edwards

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.
 
I

Ian Kelly

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.

Cheers,
Ian
 
G

Grant Edwards

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top