Solution for Project Euler's problem 7