Solution for Project Euler 40.