Python: I love Python

I've been playing with a few of the (easy) math challenges at mathschallenge.net and Python makes some of them very easy to do since it can handle large numbers. For example, challenge 16 reads:

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 21000?

This becomes a few lines of python code:
strX = str(2**1000)
listNums = [int(x) for x in list(strX)]
print "Sum of %s is %d" % (strX, sum(listNums))
>>> Sum of 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 is 1366
I don't even have to include a library to do this, it's all native.

Comments

Popular posts from this blog

Shortest Sudoku solver in Python

Downloading projecteuler.net