|XP is just a number|
(jeffa) Re: Recursionby jeffa (Chancellor)
|on Nov 15, 2000 at 00:30 UTC||Need Help??|
Depends. Each recursive call to function places another entry on the system stack. So it's possible to 'crash the stack' if you get stuck in a loop.
Some problems are best suited for recursion - like traversing a file system. You don't know how many levels you will descend, so leave that up to recursion.
The same problem can be solved with a while loop, but only if you implement your own stack.
Keep in mind that the faster solution (in general - always exceptions) will be a loop, and for that matter, if you can find a formula to replace the loop, then you will really increase the speed.
Example, summing a list of consecutive numbers, starting with 1:
Both are trivial for this example - and execute in about the same time:
But, a relatively famous person by the name of Gauss will tell you that this problem can be solved in one fell swoop with: