Solution to Project Euler's problem 8