Question

In: Computer Science

The Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21…… starts with two 1s, and...

The Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21…… starts with two 1s, and each term afterward is the sum of its two predecessors. Please write a function, Fib(n), which takes n as the input parameter. It will return the n-th number in the Fibonacci sequence. Using R, the output for Fib(9) should give only the 9th element in the sequence and not any of the previous elements. Please Help :)

Solutions

Expert Solution

SOURCE CODE: in R language

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.
==========

Fib <- function(nterms)

{

# first two terms

n1 = 1

n2 = 1

count = 2

while(count < nterms)

{

# Calculate the sum of last two numbers

nth = n1 + n2

# update values

n1 = n2

n2 = nth

count = count + 1

}

# Return the nth fibonacci number

return (nth)

}

# test the function here with different values

print(Fib(5))

print(Fib(7))

print(Fib(10))

print(Fib(15))

===================


Related Solutions

The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13,...
The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . . See the pattern? Each element in the series is the sum of the preceding two elements. Here is a recursive formula for calculating the nth number of the sequence: Fib(N) = {N, if N = 0 or 1 Fib(N - 2) + Fib(N - 1), if N > 1 a) Write a recursive method fibonacci that returns...
The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the...
The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. Write a non-recursive function Fibonacci (n) that calculates the nth Fibonacci number. Write a program to display a table of terms and the Fibonacci number in two columns for the first 15 terms, using the function you created.
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally,...
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally, it can be expressed as: fib0 = 0 fib1 = 1 fibn = fibn-1 + fibn-2 Write a multithreaded C++ program that generates the Fibonacci series using the pthread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program will generate. The program will then create a separate thread that will...
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!
The transmitter transmits either an infinite sequence of 0s with a probability 2/3 or 1s with...
The transmitter transmits either an infinite sequence of 0s with a probability 2/3 or 1s with a probability 1/3. Each symbol, regardless of the others and the transmitted sequence is identified by the receiving device with an error with a probability 0.25. i) Given that the first 5 identified symbols are 0s, find the probability P (000000 | 00000) that the sixth received symbol is also zero. b) Find the average value of a random variable equal to the number...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5. Your assignment...
Consider the Fibonacci sequence 1,1,2,3,5,8,13,21,34,55,89,…. . The first two numbers are 1 and 1. When you...
Consider the Fibonacci sequence 1,1,2,3,5,8,13,21,34,55,89,…. . The first two numbers are 1 and 1. When you add these numbers you get 2 = 1+1, which becomes the third number in the sequence. When you add the second and third numbers, you get 3 = 1+2, which becomes the fourth number in the sequence. When you add the third and fourth numbers, you get 5 = 2+3, which becomes the fifth number in the sequence; and so on to generate the...
For the Fibonacci sequence, prove the formula u2n+1 = un un+2 + (-1)n
For the Fibonacci sequence, prove the formula u2n+1 = un un+2 + (-1)n
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...
Month 1 2 3 4 5 6 7 Value 23 13 20 12 18 21 16...
Month 1 2 3 4 5 6 7 Value 23 13 20 12 18 21 16 (b) Develop a three-month moving average for this time series. Compute MSE and a forecast for month 8. If required, round your answers to two decimal places. Do not round intermediate calculation. MSE:   The forecast for month 8:   (c) Use α = 0.2 to compute the exponential smoothing values for the time series. Compute MSE and a forecast for month 8. If required, round...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT