Question

Below you will find a recursive function that computes a Fibonacci sequence (Links to an external...

Below you will find a recursive function that computes a Fibonacci sequence (Links to an external site.).  

# Python program to display the Fibonacci sequence up to n-th term using recursive functions

def recur_fibo(n):
"""Recursive function to
print Fibonacci sequence"""
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))

# Change this value for a different result
nterms = 10

# uncomment to take input from the user
#nterms = int(input("How many terms? "))

# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))

Group Discussion

How does the performance of this recursive functions compare to that of an iterative version?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code for the Fibonacci sequence:-

#Recursive method to find the fibonacci sequence
def fibonacci_sequence(list1,n):
if n==0:
return list1
length=len(list1)
list1.append(list1[length-1]+list1[length-2])
return fibonacci_sequence(list1,n-1) #Recursive call
  
# Method to display the fibonacci sequence
def disp(list1):
for i in list1:
print(i,end=" ")
def main():
list1=[]
n=int(input("Enter the value of n: ")) # User input the value of n
print("Fibonacci Sequence: ",end=" ")
if n==1:
list1.append(0)
disp(list1)
elif n==2:
list1.append(0)
list1.append(1)
disp(list1)
else:
list1.append(0)
list1.append(1)
list1=fibonacci_sequence(list1,n-2)
disp(list1)
  
if __name__=='__main__':
main() #Calling main method


The Above Recursive implementation for finding a Fibonacci sequence performance is the same as that of the iterative approach Both approach time Complexity and Space Complexity is O(n).

The only difference is the recursive approach uses recursion tree to produce Fibonacci series

Add a comment
Know the answer?
Add Answer to:
Below you will find a recursive function that computes a Fibonacci sequence (Links to an external...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python Write a function that takes as input a single integer parametern and computes the nth...

    python Write a function that takes as input a single integer parametern and computes the nth Fibonacci Sequence number The Fibonacci sequence first two terms are 1 and 1(so, if n - 2 your function should return 1). From there, the previous two values are summed to come up with the next result in the sequence 1.1,2,3,5,8,13, 21, 34, 55, etc.) For example, if the input is 7 then your function should return 13 26561.1615880 1 def Fibonacci(n): # your...

  • In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int...

    In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int -> Int Write a recursive function myProduct that multiplies all the numbers in a list. myProduct :: [Integer] -> Integer Using the technique of List Comprehension write a function that would remove even numbers from a list of lists. For Example: given input: [[1,2,3,4], [6,3,45,8], [4,9,23,8]] expected output: [[1,3], [3,45],[9,23]]. Show how the library function replicate :: Int -> a-> [a] that produces a...

  • The ­following Implementation of the Fibonacci function is a correct, but inefficient, def fibonacci(n): if n...

    The ­following Implementation of the Fibonacci function is a correct, but inefficient, def fibonacci(n): if n <= 2: return 1 else: return fib(n - 1) + fib(n - 2) In more details, the code shown runs very slowly for even relatively small values of n; it can take minutes or hours to compute even the 40th or 50th Fibonacci number. The code is inefficient because it makes too many recursive calls. It ends up recomputing each Fibonacci number many times....

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

    Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • The following function computes by summing the Taylor series expansion to n terms. Write a program...

    The following function computes by summing the Taylor series expansion to n terms. Write a program to print a table of using both this function and the exp() function from the math library, for x = 0 to 1 in steps of 0.1. The program should ask the user what value of n to use. (PLEASE WRITE IN PYTHON) def taylor(x, n): sum = 1 term = 1 for i in range(1, n): term = term * x / i...

  • Fibonacci num Fn are defined as follow. F0 is 1, F1 is 1, and Fi+2 =...

    Fibonacci num Fn are defined as follow. F0 is 1, F1 is 1, and Fi+2 = Fi + Fi+1, where i = 0, 1, 2, . . . . In other words, each number is the sum of the previous two numbers. Write a recursive function definition in C++ that has one parameter n of type int and that returns the n-th Fibonacci number. You can call this function inside the main function to print the Fibonacci numbers. Sample Input...

  • The Fibonnaci sequence is a recursive sequence defined as: f0 = 1, f1 = 1, and...

    The Fibonnaci sequence is a recursive sequence defined as: f0 = 1, f1 = 1, and fn = fn−1 + fn−2 for n > 1 So the first few terms are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . .. Write a function/procedure/algorithm that computes the sum of all even-valued Fibonnaci terms less than or equal to some positive integer k. For example the sum of all even-valued Fibonnaci terms less than or equal to 40...

  • MATLAB 1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo...

    MATLAB 1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo = 0 and F1 = 1. Hence F2 = 1, F3 = 2, F4 = 3, etc. In this problem you will use three different methods to compute the n-th element of the sequence. Then, you will compare the time complexity of these methods. (a) Write a recursive function called fibRec with the following declaration line begin code function nElem = fibrec (n) end...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT