0

Fibonacci series using recursion in python

num = int(input()) def fibonacci(n): #complete the recursive function if n<=1: print(1) else: print (fibonacci (n-3)) fibonacci(num) This code doesn't work, can anyone tell me the problem? And please prive solution also that why will it work or not

7th Jun 2021, 9:50 AM
Kairav Bhatia
Kairav Bhatia - avatar
12 Respostas
+ 3
""" is recursion required by task or not? assuming it is, you should: + store the fibonacci terms serie when computing terms (and handle length limit of list used) + compute each term that is not yet stored (and store them up to list limit) and retrieve those wich are stored + get nth term by computing first (n-2)th term, then adding (n-1)th term to spare some recursivity however, task may not expect you using memoization ^^ """ fib_terms = [1,1] def fibonacci(n): x = len(fib_terms) if n < x: return fib_terms[n] if n == x: t = fib_terms[n-2] + fib_terms[n-1] fib_terms.append(t) return t return fibonacci(n-2) + fibonacci(n-1) """ but now, with that function and its buffer, recursion will never occur if you need to output all terms of serie up to n, as previous terms are always known ;P in any recursive implementation cases, function return only the nth term (until you add smart print inside, and reset buffer before external calls) so you may loop to print the serie """
7th Jun 2021, 6:05 PM
visph
visph - avatar
+ 2
Thank you soo much
7th Jun 2021, 7:37 PM
Kairav Bhatia
Kairav Bhatia - avatar
+ 1
Fibonacci of n = fibonacci(n-1) + fibonacci (n-2)
7th Jun 2021, 10:24 AM
YUGRAJ
+ 1
well, i forgot to implement the list limit: it's quite optional as may only ends to error(s) for high values of n (above the list index limit or memory limit), wich would not occur for a recursive expected solution ;) anyway, to make it quite more safe, you just have to add a fib_limit variable initialized with the maximum n limit, and change the line: if n == x: by: if n == x and x < fib_limit: then recursion only occur for n >= fib_limit in case of looping to print all serie up to n ;)
7th Jun 2021, 6:22 PM
visph
visph - avatar
+ 1
Easiest way is here:- _______________________ num = int(input()) x, y=0,1 def fibonacci(n): global x ,y if n<=0: return 0 else: print(x) x,y=y,x+y fibonacci(n-1) fibonacci(num)
7th Sep 2022, 2:59 AM
Manender Dutt
Manender Dutt - avatar
0
It gives a type error that + should not work for None type of something error
7th Jun 2021, 12:52 PM
Kairav Bhatia
Kairav Bhatia - avatar
0
You should retunr values instead of printing them
7th Jun 2021, 1:14 PM
YUGRAJ
0
num = int(input()) def fibonacci(n): #complete the recursive function if n<=1: return (1) else: return(fibonacci(n-1)+fibonacci(n-2)) print(fibonacci(num)) It outputs 8 not the series
7th Jun 2021, 1:46 PM
Kairav Bhatia
Kairav Bhatia - avatar
0
I used for loop after recusions
7th Jun 2021, 3:33 PM
YUGRAJ
0
what is the exact task description? How is supposed formatted the output? using recursion to find a specific term of fibonacci serie is not really efficient, but using recursion to output whole serie up to a specific term is the worst way to use and from quite to the much ineficient (if you use or not memoization)... the berter way would be to use last terms memoization to whole serie memoization (if you plan to display many times some fibonnacci terms) to spare time in all cases and memory in former case ^^
7th Jun 2021, 4:19 PM
visph
visph - avatar
0
Then please share the code with memorization, i dont know how to write it that way
7th Jun 2021, 5:01 PM
Kairav Bhatia
Kairav Bhatia - avatar
0
Kairav Bhatia Here's a one-liner that returns the nth fibonacci number: def fib(n, a=0, b=1): return fib(n-1, a+b, a) if n > 1 else a Here's a better approach (using iteration): def fib(n, a=0, b=1): for i in range(n-1): a, b = a+b, a return a # Hope this helps
9th Jun 2021, 5:05 AM
Calvin Thomas
Calvin Thomas - avatar