+ 1

I need help with python recursive code for Fibonacci sequence of numbers.

Using recursion in python

23rd Apr 2021, 10:13 PM
Frimpong Godfred
Frimpong Godfred - avatar
26 Réponses
+ 1
Hi! It is exactly what you wrote in your code, but start to calculate from start case: Calculate F(3): • (n = 0) => Fib(0) = n = 0 (start case) • (n = 1) => Fib(1) = n = 1 (start case) • (n = 2) => Fib(2) = Fib(n-1) + Fib(n-2) = Fib(1) + Fib(0) = 0 + 1 = 1 (the ’second’ 1) • (n = 3) => Fib(3) = Fib(n-1) + Fib(n-2) = Fib(2) + Fib(1) = 1 + 1 = 2 • (n=4) => Fib(4) = Fib(n-1) + Fib(n-2) = Fib(3) + Fib(2) = 2 + 1 = 3 • (n =5) => Fib(5) = Fib(n-1) + Fib(n-2) = Fib(4) + Fib(3) = 3 + 2 = 5 Thus: Fib(0) = 0 Fib(1) = 1 Fib(2) = 1 Fib(3) = 2 Fib(4) = 3 Fib(5) = 5
25th Apr 2021, 9:36 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
just use this formula fib(n) =fib(n-1) +fib(n-2) And for base case if n==1 or n==2 return 1
23rd Apr 2021, 10:46 PM
YUGRAJ
+ 1
if n==1 or n==2: return 1 return fibonacci(n-1)+fibonacci(n-2)
23rd Apr 2021, 10:58 PM
YUGRAJ
+ 1
Ah print the output bro -_-
23rd Apr 2021, 11:12 PM
YUGRAJ
+ 1
Oh okay. I got that idea when I used the iterative method but with the recursive one. The mistake I think I was doing was adding it straight from the return without considering the ones gotten already . Thank you.
25th Apr 2021, 9:45 PM
Frimpong Godfred
Frimpong Godfred - avatar
+ 1
Yes, if you are happy, I am too.
25th Apr 2021, 10:29 PM
Per Bratthammar
Per Bratthammar - avatar
0
It's not working
23rd Apr 2021, 11:02 PM
Frimpong Godfred
Frimpong Godfred - avatar
0
Show me your code after my line
23rd Apr 2021, 11:04 PM
YUGRAJ
0
num = int(input()) def fibonacci(n): if n <= 1: return 0 else: return fibonacci(n-1) + fibonacci(n+2) fibonacci(num)
23rd Apr 2021, 11:06 PM
Frimpong Godfred
Frimpong Godfred - avatar
0
23rd Apr 2021, 11:10 PM
Frimpong Godfred
Frimpong Godfred - avatar
0
Okay bro I have done that but it only prints one number
23rd Apr 2021, 11:14 PM
Frimpong Godfred
Frimpong Godfred - avatar
0
What you want to print whole series upto n??
23rd Apr 2021, 11:15 PM
YUGRAJ
0
I want to print the Fibonacci sequence of numbers of n.
23rd Apr 2021, 11:17 PM
Frimpong Godfred
Frimpong Godfred - avatar
0
I recommend do it using for loop
23rd Apr 2021, 11:20 PM
YUGRAJ
0
Okay Thank you , I was able to do it but I need more explanation on this code: https://code.sololearn.com/cAmndo0LuGv0/?ref=app
24th Apr 2021, 12:48 PM
Frimpong Godfred
Frimpong Godfred - avatar
0
What explaination you want??
24th Apr 2021, 1:02 PM
YUGRAJ
0
When I break the code down, I don't get understand how the 1's came twice but when I use the iterative code to find for the Fibonacci sequence, I understand it over there.
24th Apr 2021, 3:54 PM
Frimpong Godfred
Frimpong Godfred - avatar
0
But when you use range, it better use (num + 1) instead of just num. Otherwise you print all fibonacci up to n-1, but not fibonacci(n). for n in range(num + 1): print(fibonacci(n))
25th Apr 2021, 10:02 PM
Per Bratthammar
Per Bratthammar - avatar
0
Okay, let me try that one too
25th Apr 2021, 10:08 PM
Frimpong Godfred
Frimpong Godfred - avatar
0
I just tried that and I saw no difference from the other one.
25th Apr 2021, 10:09 PM
Frimpong Godfred
Frimpong Godfred - avatar