Solution to Project Euler problem 10