Check whether a number is Fibonacci or not

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.

  1. 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.
  2. 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 

Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.