r/AskComputerScience • u/Bethelyhills • Feb 27 '20
Why is recursion so praised despite it typically using more memory and not being any faster than iteration?
For example, a naive approach to calculating Fibonacci numbers recursively would yield a time complexity of O(2^n) and use up way more memory due to adding calls on the stack vs an iterative approach where the time complexity would be O(n). Even if you use dynamic programming for a recursive solution, you'd get the same time complexity as the iterative solution while using up much more memory. Especially if your iterative solution just uses an array of two elements to calculate the numbers in the sequence.