Question

Programming Exercise 11.6 Х + | Instructions fib.py >_ Terminal + iit B 1 def fib(n): 2 *Returns the nth Fibonacci number.

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

#Python program

dict = {}
def fib(n):
    dict[1] = 1
    dict[2] = 1
    def memoizedFib(n):
        if n in dict:
            return dict[n]
        else:
            dict[n] = fib(n-1) + fib(n-2)

    memoizedFib(n)
    return dict[n]


def main():
    problemSize = 2
    print("%4s%12s" %("n", "fib(n)"))
    for count in range(5):
        print("%4s%12s" %(problemSize, fib(problemSize)))
        problemSize *= 2

if __name__ == "__main__":
    main()

Add a comment
Know the answer?
Add Answer to:
Programming Exercise 11.6 Х + | Instructions fib.py >_ Terminal + iit B 1 def fib(n):...
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
  • 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....

  • Hem 1 def expo(base, exponent): # Write your function here Python's pow function returns the result...

    Hem 1 def expo(base, exponent): # Write your function here Python's pow function returns the result of raising a number to a given power. Define a function expo that performs this task, and state its computational complexity using big-o notation. The first argument of this function is the number, and the second argument is the exponent (nonnegative numbers only) 4 def main(): ***Tests with powers of 2.*** for exponent in range (5): print(exponent, expo(2, exponent)) 000 9 if _name__ ==...

  • Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number...

    Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number which is defined as follows: fibo(0) = 0 fibo(1) = 1 fibo(n) = fibo(n-1) + fibo(n-2) for n >= 2 Question 2. Write a recursive function that calculates the sum of quintics: 1power of5 + 2power of5 + 3power of5 + … + n5 Question 3. Write a program to find a route from one given position to another given position for the knight...

  • 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...

  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

  • Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_...

    Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_ Terminal + A sequential search of a sorted list can halt when the target is less than a given element in the list. 1 " 2 File: search.py 3 Project 11.1 4 5 Optimizes sequential search for sorted lists. 6 The complexity is O(n) in the worst case and 0(1) in the best case. 7 The average case is less than n / 2,...

  • Assignment 1) Your function will only write the Nth character of the array, supplied as a...

    Assignment 1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below). void writeArrayNthBackward(const char [], int, int, int); The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n...

  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

  • Exercise #1: Write a C program that contains the following steps (make sure all variables are...

    Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. Commenting out the existing coding, write the code to divide a...

  • I really need help with this python programming assignment Program Requirements For part 2, i need...

    I really need help with this python programming assignment Program Requirements For part 2, i need the following functions • get floats(): It take a single integer argument and returns a list of floats. where it was something like this def get_floats(n):   lst = []   for i in range(1,n+1):     val = float(input('Enter float '+str(i)+': '))     lst.append(val)   return lst • summer(): This non-void function takes a single list argument, and returns the sum of the list. However, it does not use...

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