Question

In: Statistics and Probability

Fibonacci Write pseudocode to calculate F(n) Write pseudocode to construct a vector of the first n...

  1. Fibonacci

    1. Write pseudocode to calculate F(n)

    2. Write pseudocode to construct a vector of the first n numbers in the Fibonacci

      sequence.

    3. Write a function:

      fibo(n = 1){

      # Your code here

      }

      which takes one parameter ​n​ and returns the ​nth​ fibonacci number.

    4. Use the function you wrote to calculate the 58th Fibonacci number. Include your

      R code in your report

  2. The Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...) is defined as F(1) = 1, F(2) = 1, F(n) = F(n-1) + F(n-2).

  3. I need the R studio using R markdown code for this

Solutions

Expert Solution

We are essentially required to compute Fibonacci numbers and add the Fibonacci numbers to a vector, and also define a function to calculate the Fibonacci number for any number 'n' we desire.

Using this function, we must then compute the 58th Fibonacci number.

A Fibonacci sequence is a sequence whose nth value is a sum of the (n-1)th and the (n-2)th value.

Thus, to add Fibonacci numbers to a vector, we can use a for-loop. The value of 'n' can be defined by the user. The code is:

n=5

# Dummy value of n

fibo=vector("numeric",n)
# Intitializing fibo vector as an empty vector

for(i in 1:n){
if(i==1){
fibo[i]=1
}else if(i==2){
fibo[i]=1
}else{
fibo[i]=fibo[i-1]+fibo[i-2]
}
}

# Defining a for loop which will keep adding the values as required

In the above code, you just have to change the value of n, and you will get a vector of Fibonacci numbers till that value of n.

Below is the code for defining a function to compute Fibonacci numbers:

fibo_cal= function(n){
if(n==1){
return(1)
}else if(n==2){
return(1)
}else{
return(fibo_cal(n-1)+fibo_cal(n-2))
}
}

fibo_cal(58)

Above is the code to compute the Fibonacci number at a specific point. fibo_cal is the name of the function we have defined, and typing in fibo_cal(58) will give us the 58th Fibonacci number.

If you have any doubts, please let me know in the comments and I will get back to you. Happy learning!


Related Solutions

2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2)...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2) with f(0) = 0 and f(1) = 1. Find f(54) by a program or maually. Note that this number must be positive and f(53) = 53.......73 (starting with 53 and ending with 73). I must admit that my three machines including a desktop are unable to find f(54) and they quit during computation. The answer is f(54) = 86267571272 */ The Java code: public...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n −...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n − 2) for n ≥ 2 (a) Use strong induction to show that F(n) ≤ 2^n for all n ≥ 0. (b) The answer for (a) shows that F(n) is O(2^n). If we could also show that F(n) is Ω(2^n), that would mean that F(n) is Θ(2^n), and our order of growth would be F(n). This doesn’t turn out to be the case because F(n)...
In Python Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the...
In Python Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the 'nxt' method. In this class, the 'val' member is a Fibonacci number. The 'nxt' method will return a 'Fibonacci' object whose value is the next number in Fibonacci series. class Fibonacci (): """A Fibonacci number. >>>a = Fibonacci(): >>>a 0 >>>a.nxt() 1 >>>a.nxt().nxt() 1 >>>a.nxt().nxt().nxt() 2 >>>a.nxt().nxt().nxt().nxt() 3 >>>a.nxt().nxt().nxt().nxt().nxt() 5 >>>a.nxt.nxt().nxt().nxt().nxt().nxt() 8 """ def __init__(self): self.val = 0 def nxt(self): """YOUR SOURCE CODE HERE"""...
Write a MIPS assembly program to calculate the Fibonacci numbers from 1..n using the recursive method....
Write a MIPS assembly program to calculate the Fibonacci numbers from 1..n using the recursive method. The definition of a Fibonacci number is F(n) = F(n-1) + F(n-2). The implementation must follow the following guidelines: Prompt the user for a number n Allocate heap memory to hold the exact number of elements in the Fibonacci sequence for n Implement recursive Fibonacci method as a subprogram Print the Fibonacci sequence array
Write the pseudocode that prompts the user for their first and last name. Display the first...
Write the pseudocode that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user. Ask the user to input a phone number. The program checks which part of Colorado a phone number is from using the values below. If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: The sequence Fn of Fibonacci numbers is defined by the recurrence relation:  which says any Nth Fibonacci number is the sum of the (N-1) and (N-2)th Fibonacci numbers. Instructions Your task will be...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Design a Turing Machine to construct the function f(n) = 4 [1/4 n] + 2, (that...
Design a Turing Machine to construct the function f(n) = 4 [1/4 n] + 2, (that is, 2 more than 4 times the integer part of 1/4 n) for n Element N. Do not just produce a TM, but also describe briefly how it works. There is a TM in the Cooper notes that does something similar. You may modify it to produce the required TM, or produce a machine totally independently.
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT