Problem 9 solution. It's a bit smarter than brute force.