Solution for Project Euler problem 27