trying to create a recursive algorithm for solving the problem