Question

Fibonacci numbers – for loops We’ll define the Fibonacci numbers to be an integer sequence starting...

Fibonacci numbers – for loops

We’ll define the Fibonacci numbers to be an integer sequence starting with 1, 1, and where each subsequent element is got by adding the previous two elements:

1, 1, 2, 3, 5, 8, . . .

(Mathematicians start with 0, 1, . . . , but we want to avoid zeros because you can’t take their log. )

Write a python programme in a Jupyter notebook which uses a for-loop to fill an array with the first 20 Fibonacci numbers. You will need to initialise the first two elements of the array before you enter the loop. Your programme should then list the Fibonacci numbers.

Finally plot a semi-log graph of the numbers versus their place in the sequence.

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

Program in python:

#importing module for creating array
import array as arr
#importing module for creating plot
import matplotlib.pyplot as pt

# create array with int type
ar = arr.array('i', [1, 1])
#inserting n more values to the array
n=20
for i in range (1, n):
    ar.insert((i+1),ar[i]+ar[i-1])

#creating list to store the index position
ls=[]
print ("Array element are: ", end =" ")
for i in range (0, len(ar)):
    #printing the array element
    print (ar[i], end =" ")
    #adding index to the list
    ls.append(i)
#calling function to plot the graph using function
pt.semilogx(ls, ar)
#labelling the x and y axis
pt.xlabel("Index")
pt.ylabel("Value")
#showing the graph to the user
pt.show()

Indentation:

Output:

Add a comment
Know the answer?
Add Answer to:
Fibonacci numbers – for loops We’ll define the Fibonacci numbers to be an integer sequence starting...
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 Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...

    The Fibonacci sequence is the sequence 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. For example, 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). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called...

  • 3[20%] | Fibonacci numbers are the numbers in a sequence in which the first two ele-...

    3[20%] | Fibonacci numbers are the numbers in a sequence in which the first two ele- ments are 0 and 1, and the value of each subsequent element is the sum of the previous two elements: 0,,,2,3, 5, 8, 13, Write a MATLAB program in a script file that determines and displays the first 20 Fibonacci numbers.

  • this is using MATLAB 2. Fibonacci sequence: A Fibonacci sequence is composed of elements created by...

    this is using MATLAB 2. Fibonacci sequence: A Fibonacci sequence is composed of elements created by adding the two previous elements. The simplest Fibonacci sequence starts with 1,1 and proceeds as follows: 1, 1, 2, 3, 5, 8, 13, . However, a Fibonacci sequence can be created with any two starting numbers. Create a MATLAB function called FL_fib_seq' (where F and L are your first and last initials) that creates a Fibonacci sequence. There should be three inputs. The first...

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

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

  • A Fibonacci sequence is a series of numbers where the next number in the series is...

    A Fibonacci sequence is a series of numbers where the next number in the series is the sum of the previous two numbers. The sequence starts with the numbers 0 and 1. Here are the first ten numbers of the Fibonacci sequence:             0 1 1 2 3 5 8 13 21 34 Write a Java program to print out the first fifteen numbers of the Fibonacci sequence using a loop. You will need three variables previous, current, and next...

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

  • USING MATLAB 7. A Fibonacci sequence is composed of elements created by adding the two previous...

    USING MATLAB 7. A Fibonacci sequence is composed of elements created by adding the two previous elements. The simplest Fibonacci sequence starts with 1, 1 and proceeds as follows: 1,1,2,3,5,8,13, So, if f(1)-1 and f(2) -1, then f(3)-2)+f(1) We can represent this pattern as f(x) - f(x-1)+f(x-2). A Fibonacci sequence can be created with any two numbers. Prompt the user to enter the first two numbers in a Fibonacci sequence and the total number of elements requested in the sequence....

  • Problem 7.8 (Explore: Fibonacci Identities). The Fibonacci numbers are a famous integer sequence:...

    discrete math Problem 7.8 (Explore: Fibonacci Identities). The Fibonacci numbers are a famous integer sequence: Fn) o 0, 1, 1,2,3, 5, 8, 13, 21, 34, 55, 89,... defined recursively by Fo 0, F1, and F F Fn-2 for n2 2. (a) Find the partial sums Fo+Fi +F2, Fo+ Fi +F2Fs, Fo + Fi + F2+Fs +F, FoF1+F2+ Fs+F4F (b) Compare your partial sums above with the terms of the Fibonacci sequence. Do you see any patterns? Make a conjecture for...

  • Question 2: (25marks) By definition, the first and second of the modified-Fibonacci numbers are 1 and 2 (e.g. h(0)=...

    Question 2: (25marks) By definition, the first and second of the modified-Fibonacci numbers are 1 and 2 (e.g. h(0)=1 and h(1)-2), and each subsequent number is the sum of the previous two. a. Write the closed form of the causal Fibonacci function h(n)? b. Write ten Fibonacci number, h(n) for n=0, 1, ,9 described by the above definition? c. Calculate the 4-FFT of the sequence in (b). d. Finally, compute the 4-point IDFT of the result and verify that you...

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