Solution to Project Euler's problem 4