我们得到了一个数组,我们的任务是检查数组元素是否存在于斐波那契数列中。如果是,则打印该元素。
例子:
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
如果(5 * n * n – 4)或(5 * n * n + 4)是一个完美的平方,则该数字被称为斐波那契数列。有关详细信息,请参考检查给定的号码是否为斐波那契数。
C++
// CPP program to find Fibonacci series numbers
// in a given array.
#include
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
PHP
Javascript
输出:
2 8 5 1 13