recursive solution for pr 31