3 ms·
I find recursive code far easier to read (>90% of the time) ... but it's often not the most optimal solution The classic, literally "textbook", example is solv
by warrenm 4y ago
I find recursive code far easier to read (>90% of the time) ... but it's often not the most optimal solution
The classic, literally "textbook", example is solving the Fibonacci sequence
Where F(N) = F(N-1) + F(N-2)
AND F(0) = 1
AND F(1) = 1
You have a pair of base cases / definitions
But then the rest of the solution is trivial to implement (even most-obviously, to some people) implemented in its recursive form :)
- marginalia_nu 4y agoFibonacci is pretty much cherry picked to make recursive definitions look good. It's not that far from a function like the Collatz sequence, which will may just earn you a Fields medal if you're able to say for certain what it will return for all given inputs.