Solution to Project Euler Problem 24