Solution to Project Euler's problem 6