0

Won't it be too many function calls in recurssion functions

In this example just passing 1 to function is_odd(1) takes 4 function calls.... i think it takes too many function calls as the input number increases, which will cause memory out issue while storing function calls in stack.. plz correct me if am wrong... thanks

2nd Dec 2016, 6:08 AM
Manch
Manch - avatar
4 odpowiedzi
+ 1
oh, this one. this method is made just for demonstration how deep and useless may recursion be. it calls two functions for each number between 0 and analyzed number and this is not good. recursion must be used only in case if you need consequent calculation of previous results and you don't want to make huge list for this. to check if number is even or odd you just need the remainder of division by 2. 0 means even, 1 means odd.
2nd Dec 2016, 11:54 AM
Demeth
Demeth - avatar
0
what does your function look like? pls provide code. the function for odd/even number shouldn't be recursive at all, that's pointless.
2nd Dec 2016, 6:57 AM
Demeth
Demeth - avatar
0
I came across this example while reading recursion in python : def is_even(x): if x == 0: return True else: return is_odd(x-1) def is_odd(x): return not is_even(x) print(is_odd(17)) print(is_even(23))
2nd Dec 2016, 7:00 AM
Manch
Manch - avatar
0
Thanks Demeth
2nd Dec 2016, 1:30 PM
Manch
Manch - avatar