Problem 13 solution