corrected algorithm for solution to problem 13