redid problem 1 to use a tail recursive function