recursive solution to P0901