0
Fibonacci code question
I used this code to solve the fibonacci task in Python core num = int(input()) def fibonacci(n): #complete the recursive function if n <= 1: return n else: return fibonacci(n-1) + fibonacci(n-2) for i in range(num): print(fibonacci(i)) Can someone explain why the whole output sequense turns negative (-1, -1,...) If n <= 1 is n<=0 instead? Was stuck on this for a while.
2 Antworten
+ 2
Just look at Fibonacci(n-2). When you have if n<=0 then for sure you will have -1 with n=1
0
Thank you! I see it now.