Fibonacci number in an array

24 Mar, 2021
We have been given an array and our task is to check if the element of array is present in Fibonacci series or not. If yes, then print that element. Examples:  
Input : 4, 2, 8, 5, 20, 1, 40, 13, 23
Output : 2 8 5 1 13
Here, Fibonacci series will be 0, 1, 1, 2, 
3, 5, 8, 13, 21, 34, 55. Numbers that are present 
in array are 2, 8, 5, 1, 13
For 2 -> 5 * 2 * 2  - 4 = 36
36 is a perfect square root of 6.

Input : 4, 7, 6, 25
Output : No Fibonacci number in this array
 
A number is said to be in Fibonacci series if either (5 * n * n – 4) or (5 * n * n + 4) is a perfect square. Please refer check if a given number is Fibonacci number for details.   
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find Fibonacci series numbers// in a given array.#include <bits/stdc++.h>using namespace std; // Function to check number is a// perfect square or notbool isPerfectSquare(int num){    int n = sqrt(num);    return (n * n == num);} // Function to check if the number// is in Fibonacci or notvoid checkFib(int array[], int n){    int count = 0;    for (int i = 0; i < n; i++) {        if (isPerfectSquare(5 * array[i] * array[i] + 4) || isPerfectSquare(5 * array[i] * array[i] - 4)) {            cout << array[i] << " ";            count++;        }    }    if (count == 0)        cout << "None present" << endl;} // Driver functionint main(){    int array[] = { 4, 2, 8, 5, 20, 1, 40, 13, 23 };    int n = sizeof(array) / sizeof(array[0]);     checkFib(array, n);     return 0;}
// Java program to find Fibonacci series numbers// in a given arrayimport java.io.*;import java.math.*; class GFG {    // Function to check number is a    // perfect square or not    static boolean isPerfectSquare(int num)    {        int n = (int)(Math.sqrt(num));        return (n * n == num);    }     // Function to check if the number    // is in Fibonacci or not    static void checkFib(int array[], int n)    {        int count = 0;        for (int i = 0; i < n; i++) {            if (isPerfectSquare(5 * array[i] * array[i] + 4) || isPerfectSquare(5 * array[i] * array[i] - 4)) {                System.out.print(array[i] + " ");                count++;            }        }        if (count == 0)            System.out.println("None Present");    }     // driver program    public static void main(String[] args)    {        int array[] = { 4, 2, 8, 5, 20, 1, 40, 13, 23 };        int n = array.length;        checkFib(array, n);    }} // Contributed by Pramod Kumar
# Python program to find# Fibonacci series numbers# in a given array. import math def isPerfectSquare(num):     n = int(math.sqrt(num))    return (n * n == num)   # Function to check if the number# is in Fibonacci or notdef checkFib(array, n):     count = 0    for i in range(n):             if (isPerfectSquare(5 * array[i] * array[i] + 4) or            isPerfectSquare(5 * array[i] * array[i] - 4)):                     print(array[i], " ", end ="");            count = count + 1                  if (count == 0):        print("None present");  # driver codearray = [4, 2, 8, 5, 20, 1, 40, 13, 23]n = len(array)  checkFib(array, n)  # This code is contributed# by Anant Agarwal.
// C# program to find Fibonacci series// numbers in a given arrayusing System; class GFG {         // Function to check number is a    // perfect square or not    static bool isPerfectSquare(int num)    {        int n = (int)(Math.Sqrt(num));        return (n * n == num);    }     // Function to check if the number    // is in Fibonacci or not    static void checkFib(int[] array, int n)    {        int count = 0;        for (int i = 0; i < n; i++) {            if (isPerfectSquare(5 * array[i] * array[i] + 4) ||                isPerfectSquare(5 * array[i] * array[i] - 4))            {                Console.Write(array[i] + " ");                count++;            }        }        if (count == 0)            Console.WriteLine("None Present");    }     // driver program    public static void Main()    {        int[] array = { 4, 2, 8, 5, 20, 1, 40, 13, 23 };        int n = array.Length;        checkFib(array, n);    }} // This code is contributed by Sam007
<?php// PHP program to find// Fibonacci series numbers// in a given array. // Function to check// number is a perfect// square or notfunction isPerfectSquare($num){    $n = (int)(sqrt($num));    return ($n * $n == $num);} // Function to check// if the number is// in Fibonacci or notfunction checkFib($array, $n){    $count = 0;    for ($i = 0; $i < $n; $i++)    {        if (isPerfectSquare(5 * $array[$i] *                                $array[$i] + 4) ||            isPerfectSquare(5 * $array[$i] *                                $array[$i] - 4))        {            echo $array[$i]." ";            $count++;        }    }    if ($count == 0)        echo "None present\n";} // Driver Code$array = array(4, 2, 8, 5, 20,                1, 40, 13, 23);$n = sizeof($array); checkFib($array, $n); // This code is contributed by mits. ?>
<script>// Javascript program to find// Fibonacci series numbers// in a given array. // Function to check// number is a perfect// square or notfunction isPerfectSquare(num){    let n = parseInt(Math.sqrt(num));    return (n * n == num);} // Function to check// if the number is// in Fibonacci or notfunction checkFib(array, n){    let count = 0;    for (let i = 0; i < n; i++)    {        if (isPerfectSquare(5 * array[i] *                                array[i] + 4) ||            isPerfectSquare(5 * array[i] *                                array[i] - 4))        {            document.write(array[i] + " ");            count++;        }    }    if (count == 0)        document.write("None present + <br>");} // Driver Codelet array = [4, 2, 8, 5, 20,                1, 40, 13, 23];let n = array.length; checkFib(array, n); // This code is contributed by _saurabh_jaiswal</script>
Output:  
2 8 5 1 13
This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
Mithun Kumar
_saurabh_jaiswal
Fibonacci
Arrays
Mathematical
Arrays
Mathematical
Fibonacci
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Write a program to reverse an array or string
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Largest Sum Contiguous Subarray
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays