Finally, just a few hours after my previous post I have reached my goal. Less than a month ago I started solving Euler problems from ProjectEuler.net. In my mind I set a goal to solve 30 problems and today I can proudly say that I successfully did it. Some more thoughts about the experience later in the post, but now - the last problem.

Problem 63

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power. How many n-digit positive integers exist which are also an _n_th power?

My solution:

count = 0
for a in range(1, 1000):
	for b in range(1, 1000):
		if(len(str(a ** b)) == b):
			count += 1
print count

My first thought to solve this was to use logarithms, but obviously it’s the wrong way. Then, by choosing an usual bruteforce path, I realized that I can generate base and power and don’t care about the resulting number - it can be calculated. a ** b is converted to string to get the length of the number (len function does not handle integers) and compared to power. If it matches the counter is incremented.

And finally - the experience. The problems are really interesting and it was even addicting to solve them. Actually I was surprised how fun it was to solve the problems - there were quite a few “A-ha!” moments, I got to compare my knowledge to the rest of the world, increase the number of Python users and contribute to Lithuania’s score (at the moment there are 251 Lithuanian solvers. I’m 69th on the list) on ProjectEuler.

I have solved first 14 problems in a row. It’s the most consecutive number of solutions I’ve got there so far. I thought I was going strong, but it wasn’t the case. After that I skipped a whole lot of problems, generally because I came up with and followed this rule: If I can’t come up with an algorithm to solve the problem in 3 minutes, I will skip it. Also I skipped some for no apparent reason.

Problems I solved

I certainly haven’t checked out all of the problems in the project, so I believe there is quite a bit of room to improve my score. I shot for the simplest and neatest solutions I could came up with at that time and I am pretty happy with them.

Was it worth the time and efforts? I think it was. It was a great chance to dive a bit more into Python, check my logic. On top of that - it was really fun. It consumed roughly half a week of work throughout the month.

I would recommend you to check it out, see how well you can do. You should learn something. If you are interested, you can find the project at ProjectEuler.net

Thank you for reading that that’s it for now.