Question

Write a function that checks if a number is in the Fibonacci sequence using recursion. In...

Write a function that checks if a number is in the Fibonacci sequence using recursion. In C++.

Homework Answers

Answer #1
#include <iostream>

using namespace std;

bool is_in_fibonacci_sequence(int n, int f0 = 0, int f1 = 1) {
    if (n < f0) {
        return false;
    } else if (n == f0) {
        return true;
    } else {
        return is_in_fibonacci_sequence(n, f1, f0 + f1);
    }
}

int main() {
    int n;
    cout << "Enter a positive integer: ";
    cin >> n;

    if (is_in_fibonacci_sequence(n)) {
        cout << n << " is in the fibonacci sequence" << endl;
    } else {
        cout << n << " is not in the fibonacci sequence" << endl;
    }
    return 0;
}

Know the answer?
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
Similar Questions
Write in a c++ recursion function to prints the digits in reverse order using a recursion...
Write in a c++ recursion function to prints the digits in reverse order using a recursion function. Input: 23567 Output: 76532
Write a method in JAVA/C++ to solve the Syracuse Sequence using recursion. A Syracuse Sequence is...
Write a method in JAVA/C++ to solve the Syracuse Sequence using recursion. A Syracuse Sequence is a sequence that begins with a number n0 and each element ni of the sequence is ni-1/2 if is ni-1 even and 3*ni-1+1 otherwise. You can write the method in a .docx file (no source code required). Also write the base, and induction clause. Draw the Activation Record (AR) diagram for the first six elements of the series when it starts with 7 (the...
For the Fibonacci sequence, with the recursion relation un+1=un + un-1, make the model un =...
For the Fibonacci sequence, with the recursion relation un+1=un + un-1, make the model un = rn. Determine r
Please Answer In Details Show all Steps: Implement two C++ programs to generate Fibonacci sequence (one...
Please Answer In Details Show all Steps: Implement two C++ programs to generate Fibonacci sequence (one using recursion, one using non-recursion structure); analyze their complexity using O notation
In C++ Using recursion write a program that takes a positive integer number and returns whether...
In C++ Using recursion write a program that takes a positive integer number and returns whether there is 6. For example, if the input number is 7068, the function returns true
Write a c++ program that evaluates the first 50 numbers of the Fibonacci sequence and store...
Write a c++ program that evaluates the first 50 numbers of the Fibonacci sequence and store the numbers in an array. The c++ program asks the user for an input to enter a positive integer 'n' and display the nth fibonacci number "FibN" that is stored inside of the array
Given: function f = fibonacci(n) % FIBONACCI Fibonacci sequence % f = FIBONACCI(n) generates the first...
Given: function f = fibonacci(n) % FIBONACCI Fibonacci sequence % f = FIBONACCI(n) generates the first n Fibonacci numbers. f = zeros(n,1); f(1) = 1; f(2) = 2; for k = 3:n f(k) = f(k-1) + f(k-2); end AND function f = fibnum(n) if n <=1 f =1; else f = fibnum(n-1) + fibnum(n-2); end In Matlab, modify fibonacci.m and fibnum.m to compute the following sequence. dn = 0, n<=0 d1 = 1, d2 = 1 and, for n >...
Write a VSC (macro) program that computes and displays a Fibonacci sequence. A Fbonacci sequence is...
Write a VSC (macro) program that computes and displays a Fibonacci sequence. A Fbonacci sequence is generated by adding the two most recent sequence numbers together, i.e., 1, 1, 1+1-2, 1+2=3, 2+3=5, 3+5=8, … The user will enter the number of terms in the sequence to be displayed. Assemble this program using the VSC assembler (ASM), and simulate this program using the VSC simulator (SIM). Include a copy of the source listing (SOURCE.DAT), the assembled listing (SLIST.DAT) and the simulation...
Write a function called "isPrime()” that checks if an integer number is prime or not. The...
Write a function called "isPrime()” that checks if an integer number is prime or not. The function returns integer 1 if the number is prime, or integer 0 if the number is not prime. Write a simple test program in main() to show that the function works properly.in C programming language.
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci...
In mathematics, the 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: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … The sequence Fn of Fibonacci numbers is defined by the recurrence relation: Fn = Fn-1 + Fn with seed values F1 = 1 F2 = 1 For more information on...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT