Recursive and iterative implementation