+ 1
How can i implement recursion on this code
I make a Fibonacci Sequence that take range number and return the sequence in this range , but i used for loop in func , i need know how can use recursion of the function it self to do the same job ? https://code.sololearn.com/ctMU2Hy6iqoT/?ref=app
1 Resposta
+ 2
Check out my fibr function below. That is recursive and behaves in a similar way. I kept your fib function as you defined it so you can compare output from my fibr with your fib next to each other.
I'm not sure why you printed the return value from your fib function when it was None. I assumed there wasn't a big reason so I used the return value in the new recursive function to help it work.
num = int(input())
def fib(n):
x=0
y=1
for i in range(n):
print(x)
hold=x+y
x=y
y=hold
def fibr(n):
if n <= 1:
print(0)
return (0, 1)
else:
n1, n2 = fibr(n - 1)
print(n2)
return (n2, n1 + n2)
fib(num)
print('---')
fibr(num)