recursive solution to P0007