Is it better to use recursive or loops? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

Is it better to use recursive or loops?

I made a code with an example where a function can be done in both ways. In a real case, which option should I go to? https://code.sololearn.com/cHuWCSn0imCY/?ref=app

1st May 2019, 10:23 PM
InvBoy [ :: FEDE :: ]
InvBoy [ :: FEDE :: ] - avatar
22 Answers
+ 15
Both were alright look at this code by Serena Yvonne if you want to look at the time they use https://code.sololearn.com/cbjU93D4nCR4/?ref=app
2nd May 2019, 1:44 AM
good_bits
good_bits - avatar
+ 11
both are okay, but if you want to go for speed, loop is faster than recursive function
1st May 2019, 11:48 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 10
I think loop is better than recursive. Recursive is good but loop is faster.
2nd May 2019, 2:30 AM
Nasir❤
Nasir❤ - avatar
+ 8
Werg Serium What about the link code?
1st May 2019, 10:40 PM
InvBoy [ :: FEDE :: ]
InvBoy [ :: FEDE :: ] - avatar
+ 6
If your preferred language is available on pythontutor.com, I recommend to go there and visualize some recursive codes. Then you will see how much more "overhead" you get by using recursion. Recursion usually only makes sense in some very limited situations like using a depth first search or flattening a list where using recursion really makes it easier to code the algortihms.
2nd May 2019, 6:04 AM
Thoq!
Thoq! - avatar
+ 6
Depends on what are you trying to achive
2nd May 2019, 3:45 PM
Radu Baloi
Radu Baloi - avatar
+ 6
Generally speaking for small input size and your current algorithm it shoudn't matter--I tried 900000 and it was fine. Replacing c/2 with c-2 it chokes on 2000. When calculating large values you will most likely blow the stack. Even if it's unlimited it will take a very long time. You will need to resort to iteration and/or start optimising your algorithm (like using memoization, doing less work like calculating half as much as before, etc) Even still a loop should be faster than recursion sometimes many times faster. Try calculating fibonnacci 200000 recursively--it's best not done. I'm unable to do that even with memoization-- program will crash. The iterative version (with memoization) does the same under 5s. #Similar discussion https://www.sololearn.com/discuss/1671835/?ref=app
3rd May 2019, 4:24 AM
Lord Krishna
Lord Krishna - avatar
+ 5
And rarelly an recursive function can be faster than a iterative function
1st May 2019, 11:52 PM
Werg Serium
Werg Serium - avatar
+ 5
Once you get used to recursion, it makes source code more readable (more eloquent) and easier to maintain. Then again, it is a matter of preference😊
2nd May 2019, 12:36 AM
Da2
Da2 - avatar
+ 4
Its depends of what you want to do.
1st May 2019, 10:23 PM
Werg Serium
Werg Serium - avatar
+ 4
Thoq! This is turning into debate about resource efficiency (speed & stack overflow) vs source code (eloquence) maintainability. In the end, I feel that there is no right or wrong answer, just preference and situations
2nd May 2019, 6:51 AM
Da2
Da2 - avatar
+ 4
@Da2 Yes, in principle you are right. Except that in Python (which is the language of the example codes InvBoy linked) I would strongly prefer using iterative code. Python is not really built for recursion and hits the recursion limit pretty fast.
2nd May 2019, 7:23 AM
Thoq!
Thoq! - avatar
+ 4
The point is complexity... the more complex source code gets, what is important. I do not have time for a debate.... Thank you for your candid response
2nd May 2019, 7:37 AM
Da2
Da2 - avatar
+ 4
Loops are a bit easier to debug if there is an error.
3rd May 2019, 1:46 AM
Sonic
Sonic - avatar
+ 3
Asterisk in the most of cases the loop is faster than recursive but in this case i didnt see any differente about speed.
1st May 2019, 11:51 PM
Werg Serium
Werg Serium - avatar
+ 3
*AsterisK* how loop is faster than recursive function
2nd May 2019, 2:56 AM
PECHI MUTHU J
PECHI MUTHU J - avatar
+ 2
Loops (While is best IMHO) 😉 Recursion can cause stack overflow ....
2nd May 2019, 3:01 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Depends in many cases recursion may be reasonable. Recursion has the main advantage that in some cases will need a much shorter and simpler implementation than the iterative method. But generally, recursions are more memory hungry and execute more instructions which will make them slower.
2nd May 2019, 10:12 AM
Andrei O
Andrei O - avatar
+ 2
Aditya Choudhury that's pretty wrong,when i work with python loops never get confusing plus there's a limit to how much you can use recursion,so loops,compressions and generators are a better alternative in most cases.
3rd May 2019, 11:46 AM
Mensch
Mensch - avatar
+ 2
Loop is more faster than recursive.. Because it call the function by itself but in loop the loop is working again nd again until the condition is false
5th May 2019, 7:07 PM
Shashi Gupta
Shashi Gupta - avatar