Python Power

Again, python has proved to me that it can beat C in many aspects!! I learned yesterday that it stores the integers without worrying about the overflowing. It keeps on increasing the memory for larger numbers, isn’t it amazing? I thought of giving a try to see upto what it can store and I wrote a simple script which keeps on doing the power of two, and it went on and on and on and never came out!! I was really impresses. The reason being help that I would get with such a liberty to solve the project euler problems :)

One of the problem I solved using python was sigma(i^i), where i ranges from 1 to 1000. and I was supposed to print the last 10 digits of the answer. I cannot think of solving this problem using C. I wonder how will I manage to store such a data. Using unsigned long long, I can store maximum 20 digit number :) Can’t imaging how big the number will be after the summation. So the choice was obviously python, and here is the code :)

#!/usr/bin/python
# Solution to Problem 48 in projecteuler.net
ans = 0;
limit = 1000;
for i in range(1,limit):
	ans = ans + pow(i,i)
print str(ans)[-10:]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s