Question


6. (20)The Fibonacci sequence is the series of integers 0, 1, 1,2, 3, 5, 8, 13, 21, 34, 55, 89.. 1 See the pattern? Each element in the series is the sum of the preceding two items. There is a recursive formula for calculating the nth number of the sequence (the oth number if Fib(0)-0): 8 Fib(N)-/N, if N 0 or 1 ifN> 1 Fib(N-2) Fib(N-1), a. b. c. Write a recursive version of the function Fibonacci. Write a nonrecursive version of the function Fibonacci Write a program to test the recursive and iterative versions of the function Fibonacci. (Test the N for different values, such as n=5, 10, 30, 40, 50)

in C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int Fib_Rec(int n) {
   if(n == 0) {
      return 0;
   } else if(n == 1) {
      return 1;
   } else {
      return Fib_Rec(n-1) + Fib_Rec(n-2);
   }
}

int Fib_Itr(int n) {
   int a0 = 0;
   int a1 = 1;
   if(n <= 1) {
      return n;
   } else {
      int an;
      for(int i = 2; i <= n; ++i) {
         an = a0+a1;
         a0 = a1;
         a1 = an;
      }
      return an;
   }
}

int main() {
   cout << "Enter n: ";
   int n;
   cin >> n;
   cout << "Fib(" << n << ") using recursion is: " << Fib_Rec(n) << endl;
   cout << "Fib(" << n << ") using non-recursion is: " << Fib_Itr(n) << endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
in C++ 6. (20)The Fibonacci sequence is the series of integers 0, 1, 1,2, 3, 5,...
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
  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series 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. 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), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

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

  • Below you will find a recursive function that computes a Fibonacci sequence (Links to an external...

    Below you will find a recursive function that computes a Fibonacci sequence (Links to an external site.).   # Python program to display the Fibonacci sequence up to n-th term using recursive functions def recur_fibo(n): """Recursive function to print Fibonacci sequence""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) # Change this value for a different result nterms = 10 # uncomment to take input from the user #nterms = int(input("How many terms? ")) # check if the number...

  • C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a...

    C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a simple pattern: 1,1, 2, 3, 5, 8, 13, 21, 34, 55, By definition, the first two numbers are 1 and 1, and each subsequent number is the sum of the previous two. For your program, you will ask the user to enter a number, n, and then calculate and display the values of the Fibonacci sequence to the nth position. ==sample output== Enter an...

  • 1.Take this recursive Fibonacci implementation and convert it into the caching based version discussed in class....

    1.Take this recursive Fibonacci implementation and convert it into the caching based version discussed in class. Implement your caching to store a maximum of 5 values. Create 2 variations: one that stores all values and one that only stores even values. Make sure that you don't leave any gaps in your cache — if you have 5 cache entries you must cache 5 unique and valid values. Compare the caching implementations to the recursive implementation using the time utility. How...

  • 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 Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .......

    The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... It is defined by the following mathematical expression, with X0 & X1 being 0 and 1, respectively: Xn = Xn-1 + Xn-2 Write a C program using the fork() system call that generates and prints the Fibonacci sequence in the child process. The number of members in the sequence will be determined by a user provided as a user prompted input. Make the parent...

  • Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formal...

    Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formally, it can be expressed as: fib0-0 fibl-1 fibn-fibn-1+fibn-2 Write a multithreaded program that generates the Fibonacci sequence. This program should work as follows: On the command line, the user will enter the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in...

  • Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

    Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...

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