Question

Using Python, create program to count number of recursive steps for Fibonacci(30) and just submit the number.

Using Python, create program to count number of recursive steps for Fibonacci(30) and just submit the number.

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

By using a global variable outside the function, and updating each time the function is called will give the number of recursive steps it takes to get the output:

Python code:

# Function for nth Fibonacci number
def Fibonacci(n):
global count
if n<0:
count = count+1
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
count = count+1
return 0
# Second Fibonacci number is 1
elif n==2:
count = count+1
return 1
else:
count = count+1
return Fibonacci(n-1)+Fibonacci(n-2)

# Driver Program
count =0
print("30th number of Fibonacci series: ",Fibonacci(30))
print("Number of Steps: ",count)

Output:

Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on 1linux 30th number of Fibonacci series: 514229 Number of Steps: 166

# Function for nth Fibonacci number def Fibonacci(n): global count if n<e: count count+1 print( Incorrect input) # First Fib

Add a comment
Know the answer?
Add Answer to:
Using Python, create program to count number of recursive steps for Fibonacci(30) and just submit the number.
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
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
ADVERTISEMENT