improved efficiency of solution to problem 14