In your above code repeated below you used straight recursion. 같은 함수를 꼬리 재귀 방식으로 재작성한 것과 비교해 보세요. With an optimising compiler the tail recursive version would be comparable to an iterative approach in performance, while retaining its arguable elegance.