Fibonacci number in an array
Last Updated :
19 Sep, 2023
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 = 16
16 is a perfect square of 4
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.
Implementation:
C++
// 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 not
bool isPerfectSquare(int num)
{
int n = sqrt(num);
return (n * n == num);
}
// Function to check if the number
// is in Fibonacci or not
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)) {
cout << array[i] << " ";
count++;
}
}
if (count == 0)
cout << "None present" << endl;
}
// Driver function
int 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
// Java program to find Fibonacci series numbers
// in a given array
import 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
Python3
# 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 not
def 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 code
array = [4, 2, 8, 5, 20, 1, 40, 13, 23]
n = len(array)
checkFib(array, n)
# This code is contributed
# by Anant Agarwal.
C#
// C# program to find Fibonacci series
// numbers in a given array
using 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
JavaScript
<script>
// Javascript program to find
// Fibonacci series numbers
// in a given array.
// Function to check
// number is a perfect
// square or not
function isPerfectSquare(num)
{
let n = parseInt(Math.sqrt(num));
return (n * n == num);
}
// Function to check
// if the number is
// in Fibonacci or not
function 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 Code
let 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>
PHP
<?php
// PHP program to find
// Fibonacci series numbers
// in a given array.
// Function to check
// number is a perfect
// square or not
function isPerfectSquare($num)
{
$n = (int)(sqrt($num));
return ($n * $n == $num);
}
// Function to check
// if the number is
// in Fibonacci or not
function 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.
?>
Similar Reads
Nth Fibonacci Number Given a positive integer n, the task is to find the nth Fibonacci number.The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21Example:Input:
15+ min read
Nth Even Fibonacci Number Given a value n, find the n'th even Fibonacci Number.Examples : Input n = 3Output 34 Explanation The first 3 even Fibonacci numbers are 0, 2, 8, 34, 144, with the 3rd being 34.Input n = 4 Output 144 Explanation The first 4 even Fibonacci numbers are 0, 2, 8, 34, 144, with the 4th being 144.[Naive Ap
5 min read
Non Fibonacci Numbers Given a positive integer n, the task is to print the nth non-Fibonacci number. The Fibonacci numbers are defined as: Fib(0) = 0Fib(1) = 1for n >1, Fib(n) = Fib(n-1) + Fib(n-2)First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, â¦â¦.. Examples: Input : n = 2Output : 6Inpu
15 min read
Alternate Fibonacci Numbers Give a number N, print alternate fibonacci numbers till n-th Fibonacci. Examples: Input : N = 7 Output : 0 1 3 8 Input : N = 15 Output : 0 1 3 8 21 55 144 377 Read method 2 in the following article : fibonacci number Approach:Using dynamic programming approach. Keep storing the previously calculated
4 min read
N-bonacci Numbers You are given two integers N and M, and print all the terms of the series up to M-terms of the N-bonacci Numbers. For example, when N = 2, the sequence becomes Fibonacci, when n = 3, sequence becomes Tribonacci. In general, in N-bonacci sequence, we use sum of preceding N numbers from the next term.
14 min read