0
Is recursion acts as a for loop....
3 Respostas
+ 1
I don't really know about other languages, but recursion in c++ is slower than for loop
Well, without some exit points
But mostly recursion is slower
0
But is recursion only used to find factorial... If not give any other example..
0
The factorial is simplest example. For example, DFS algorithm is recursive (the algorithm to go through graph)
Or segment tree
It is true that some task can be completed both by recursion and for loop
But some tasks is simpler in recursive form
And recursion is better when you need to calculate something that needs several different operation
For example, if pattern is:
If n > 0 && n < 100
Return func(n - 10) + func(n + 20)
Else
Return n
This looks easier in recursion
Buuut, funny fact, the segment of programing where you replace recursion with loop is called "dynamic programing"