Is Fibo

You are given an integer, N. Write a program to determine if N is an element of the Fibonacci sequence.

The first few elements of the Fibonacci sequence are . A Fibonacci sequence is one where every element is a sum of the previous two elements in the sequence. The first two elements are 0 and 1.

fib(0) =  0

fib(1) =1

...

fib(n)= fin(n-1)+fib(n-2)


Example:

Input:  n = 5
Output: IsFibo

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string isFibo(long n)
{

    long long int fib[51];
    fib[0] = 0;
    fib[1] = 1;
    set<long long intst;
    for (long long int i = 2i <= 50i++)
        fib[i] = fib[i - 1] + fib[i - 2];
    for (long long int i = 0i <= 50i++)
        st.insert(fib[i]);

    if (st.find(n!= st.end())
        return "IsFibo";
    else
        return "IsNotFibo";
}

int main()
{

    long n = 5;

    cout << isFibo(n<< "\n";

    return 0;
}


No comments:

Post a Comment