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)...
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part...
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part of a code public class FS { public static void main(String[] args){ IntSequence seq = new FibonacciSequence(); for(int i=0; i<20; i++) { if(seq.hasNext() == false) break; System.out.print(seq.next()+" "); } System.out.println(" "); } } ///Fill in the rest of the code! Output 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 You should define...
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
Fibonacci (C++) Generate 1st n fibonacci numbers: std::vector<int> v = {1, 1, 2, 3, 5, 8,...
Fibonacci (C++) Generate 1st n fibonacci numbers: std::vector<int> v = {1, 1, 2, 3, 5, 8, 13, 21}; auto w = fibonacci(8); Note: NO LOOPS ALLOWED Thanks!
a) write a program to compute and print n, n(f), f(f(n)), f(f(f(n))).........for 1<=n<=100, where f(n)=n/2 if...
a) write a program to compute and print n, n(f), f(f(n)), f(f(f(n))).........for 1<=n<=100, where f(n)=n/2 if n is even and f(n)=3n+1 if n is odd b) make a conjecture based on part a. use java
Write a program that will calculate the sum of the first n odd integers, and the...
Write a program that will calculate the sum of the first n odd integers, and the sum of the first n even integers. Use a Do while loop Use a for loop Here is a sample of how the program should run: How many odd integers would you like to add? 5 Count Odd Integer Sum 1 1 1 2 3 4 3 5 9 4 7 16 5 9 25 The sum of the first 5 odd integers is...
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...
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence....
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence. A Fibonacci sequence is defined as the element 1, followed by another 1, and each element thereafter is the sum of the previous two elements. For example, the first 9 elements of a Fibonacci sequence are: 1 2 3 5 8 13 21 34 This famous sequence was originally used to predict the growth of rabbit populations! Once you have each of the functions...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT