Solution for Project Euler's problem 3