5 ms·
> Fibbonacci allows us to see that they have basic recursion understanding I see no reason to use recursion when asked to calculate Fibonacci numbers. A loop l
by pps43 6y ago
> Fibbonacci allows us to see that they have basic recursion understanding
I see no reason to use recursion when asked to calculate Fibonacci numbers. A loop looks like a more reasonable choice that also avoids typical pitfalls associated with recursion. Maybe that's because I did embedded programming for a while.
I suspect recursion is introduced in CS classes with this example, but people understand it as "you are supposed to use recursion to calculate Fibonacci numbers".
- paulryanrogers 6y agoRecursion is often less efficient but looks more elegant and simpler. It breaks the problem down to is essence. Then you can trade some complexity for more run-time efficiency.
- pps43 6y ago> more elegant and simpler Beauty is in the eye of the beholder, but a loop is hard to beat as far as simplicity goes, and you don't depend on your compiler being clever enough to optimize tail recursion. If you need to traverse a tree then sure, but with Fibonacci you don't even need the stack to begin with. You only need to keep a previous number.
- deleted 6y ago[deleted]
- duskwuff 6y agoYou don't even need a loop. Binet's formula gives a closed-form expression for the Fibonacci sequence. (And of course I don't remember that formula off the top of my head, but I know it exists, so I'd be 90% of the way to solving a problem which required it.)
- pps43 6y agoThis will open another can of worms (floating point errors).