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 int> st;for (long long int i = 2; i <= 50; i++)fib[i] = fib[i - 1] + fib[i - 2];for (long long int i = 0; i <= 50; i++)st.insert(fib[i]);if (st.find(n) != st.end())return "IsFibo";elsereturn "IsNotFibo";}int main(){long n = 5;cout << isFibo(n) << "\n";return 0;}
No comments:
Post a Comment