# Search a Fibonacci number

Here, we are going to learn how to **search a Fibonacci number using searching algorithm using C++ program**?

Submitted by Radib Kar, on November 10, 2018

**Description:**

We are often used to generate Fibonacci numbers. But in this article, we are going to learn about **how to search Fibonacci numbers in an array**?

**Introduction:**

Fibonacci numbers are often used in mathematics and computer science fields. Fibonacci numbers are often considered as an important part of number theory because of their some amazing properties and the connection with the golden ratio. We are all familiar with Fibonacci number generation using dynamic programming or simple Fibonacci property. But to check a number whether it's part of Fibonacci series or not is something really challenging.

## Algorithms to search for Fibonacci numbers

The searching algorithm is linear search but what is challenging is to check for the number whether Fibonacci or not.

**Brute force**

The brute force approach is to generate the Fibonacci series and to store that in an array. We need to generate the Fibonacci series till we cover the maximum element of the search array. Then we need to check each element of the search array whether it's part of the new array consisting generated Fibonacci series. Needless to say, the brute force approach is not going to work for larger values since the complexity is much higher and the complexity also includes Fibonacci series generation which is an additional task here.**Using Mathematical formula**

Fibonacci numbers have an amazing property and one of the property is that for every Fibonacci number**n**,**5n2+4**or**5n2-4**is a perfect square.

Such property has made the checking possible in only**O(1)**time complexity and we don't need any additional storage.

## C++ implementation for searching Fibonacci numbers

#include <bits/stdc++.h> using namespace std; int isSquare(int k){ // if k isn't perfect square then the square root //will be a float value but we are rounding it off to integer int s=sqrt(k); // only in case of perfect square there //will not be any rounding off error if(s*s==k) return 1; else return 0; } int checkFibo(int k){ //checking whether (5n^2+4) or (5n^2-4) is perfect square if(isSquare(5*k*k-4)||isSquare(5*k*k+4)) return 1; else return 0; } void findFibos(int* a, int n){ int count=0; for(int i=0;i<n;i++){ if(checkFibo(a[i])){ cout<<a[i]<<" "; count++; } } if(count) cout<<"\nabove "<<count<<" fibonacci numbers are present in the array\n"; else cout<<"\nno fibonacci number is present in the array"; } int main(){ int n; // enter array length cout<<"enter no of elements\n"; cin>>n; int* a=(int*)(malloc(sizeof(int)*n)); //fill the array cout<<"enter elements................\n"; for(int i=0;i<n;i++) scanf("%d",&a[i]); findFibos(a,n); return 0; }

**Output (first run)**

enter no of elements 6 enter elements................ 2 3 10 13 15 21 2 3 13 21 above 4 fibonacci numbers are present in the array

**Output (second run)**

enter no of elements 5 enter elements................ 6 7 11 12 14 no fibonacci number is present in the array

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.