Solution for Project Euler problem 22