solve problem 47, seems too slow