completed problem 2, completed problem 1 recursively