solve 349b using recursion