Question

CODE IN PYTHON 2.7: USE GENERATORSThe Fibonacci numbers are defined by the following recursion: with initial values. Using generators, compute the first ten Fibonacci numbers, [1,1,2,3,5, 8,13,21,34,55] def fibonacci(n): F, = Fn-1 + Fn-2 In # YOUR CODE HERE raise NotImplementedError)

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

def finonacci(n): #put 10 in place of n to find first 10 fibonacci number

a,b=0,1 #initialize a by 0 and b by 1

while True:

a, b=b, a + b

yield a # return fibonacci number

raise NotImplementedError()

Add a comment
Know the answer?
Add Answer to:
CODE IN PYTHON 2.7: USE GENERATORS The Fibonacci numbers are defined by the following recursion: with...
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
  • Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the...

    Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the linear recurrence equation Fn F-1 F-2 where F F2 1 and by convention Fo 0. For example, the first 8 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21. (a) For a given n, compute the nth Fibonnaci number using a for loop (b) For a given n, compute the nth Fibonnaci number using a while loop Print the 15th Fibonacci number...

  • Recall from class that the Fibonacci numbers are defined as follows: fo = 0,fi-1 and for all n fn...

    Recall from class that the Fibonacci numbers are defined as follows: fo = 0,fi-1 and for all n fn-n-1+fn-2- 2, (a) Let nEN,n 24. Prove that when we divide In by f-1, the quotient is 1 and the remainder is fn-2 (b) Prove by induction/recursion that the Euclidean Algorithm takes n-2 iterations to calculate gcd(fn,fn-1) for n 2 3. Check your answer for Question 1 against this. Recall from class that the Fibonacci numbers are defined as follows: fo =...

  • 3. The sequence (Fn) of Fibonacci numbers is defined by the recursive relation Fn+2 Fn+1+ F...

    3. The sequence (Fn) of Fibonacci numbers is defined by the recursive relation Fn+2 Fn+1+ F for all n E N and with Fi = F2= 1. to find a recursive relation for the sequence of ratios (a) Use the recursive relation for (F) Fn+ Fn an Hint: Divide by Fn+1 N (b) Show by induction that an 1 for all n (c) Given that the limit l = lim,0 an exists (so you do not need to prove that...

  • In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.......

    In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.... In mathematical notation the sequence Fn of Fibonacci number is defined by the following recurrence relation: Fn=Fn-1+Fn-2 With the initial values of F0=0 and F1=1. Thus, the next number in the series is the sum of the previous two numbers. Write a program that asks the user for a positive integer N and generate the Nth Fibonacci number. Your main function should handle user...

  • c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8,...

    c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21 Note that the first Fibonacci number is 1, F(1) = 1 The second Fibonacci number is 1, i.e. F(2) = 1 Other Fibonacci numbers in the sequence is the sum of two previous Fibonacci numbers. For example F(3) = F(2) + F(1). In general F(n) = F(n-1) + F(n-2) Write a program to do the following tasks. User entries are shown in...

  • The Fibonacci numbers are defined as follows, f1=1, f2=1 and fn+2=fn+fn+1 whenever n>= 1. (a) Characterize...

    The Fibonacci numbers are defined as follows, f1=1, f2=1 and fn+2=fn+fn+1 whenever n>= 1. (a) Characterize the set of integers n for which fn is even and prove your answer using induction (b) Please do b as well. The Fibonacci numbers are defined as follows: fi -1, f21, and fn+2 nfn+1 whenever n 21. (a) Characterize the set of integers n for which fn is even and prove your answer using induction. (b) Use induction to prove that Σ. 1...

  • use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can...

    use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....

  • The following is a general method for finding the expresion for the Fibonacci numbers well number...

    The following is a general method for finding the expresion for the Fibonacci numbers well number of other similar problems. Let (XN) be a sequence of numbers which are defined by the reusion relation XypXn-1+qXN-2. X, X, arbitrary then clear that once Xo and X are given, one can calculate using this relation the values of X. X.... recursively (hence the name recursion relation). Here P and are given numbers For the Fibonacci sequence, p =q=1 and Xo =0,X; =...

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

  • USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key:...

    USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key: int, value: int) Description: You're done with stats, but you still have other math homework to go! You're currently learning about the Fibonacci sequence in math class. The Fibonacci sequence is a series of numbers in the pattern 112 3 5 8 13 21 ..., where the next number is found by adding up the two numbers before it. Write a function that takes...

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