0

Is recursion acts as a for loop....

16th Feb 2019, 7:07 PM
¿°°ANONYMOUS™~
¿°°ANONYMOUS™~ - avatar
3 ответов
+ 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
16th Feb 2019, 7:19 PM
Дмитрий Мазыло
Дмитрий Мазыло - avatar
0
But is recursion only used to find factorial... If not give any other example..
16th Feb 2019, 7:21 PM
¿°°ANONYMOUS™~
¿°°ANONYMOUS™~ - avatar
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"
16th Feb 2019, 7:29 PM
Дмитрий Мазыло
Дмитрий Мазыло - avatar