📌  相关文章
📜  检查数组中所有可被K整除的复合数字的GCD是否为斐波那契数

📅  最后修改于: 2021-05-17 18:30:51             🧑  作者: Mango

给定由N个非负整数和整数K组成的数组arr [] ,任务是检查数组中所有可被K整除的复合数字的GCD是否为斐波那契数。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

方法:请按照以下步骤解决问题:

  1. 创建一个函数isComposite()来检查数字是否为合成数字。
  2. 创建另一个函数isFibonacci()来检查数字是否为斐波那契数字。
  3. 初始化一个整数向量,即Compositeset ,并初始化另一个整数变量gcd,以存储数组中可由K整除的复合数的gcd。
  4. 遍历数组arr []
  5. 对于每个元素arr [i] ,检查它是否是合成的并且可以被K整除。如果发现为真,则将其插入向量复合集
  6. 计算矢量复合集中所有元素的GCD并将其存储在变量gcd中
  7. 检查gcd是否为斐波那契数。
  8. 如果发现是真的,则打印“是” 。否则,打印“否”。

下面是上述方法的实现:

C++
// C++ Program for the above approach
 
#include 
using namespace std;
 
// Function to check if a
// number is composite or not
bool isComposite(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
 
    if (n <= 3)
        return false;
 
    // Check if the number is
    // divisible by 2 or 3 or not
    if (n % 2 == 0 || n % 3 == 0)
 
        return true;
 
    // Check if n is a multiple of
    // any other prime number
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
 
            return true;
 
    return false;
}
 
// Function to check if a number
// is a Perfect Square or not
bool isPerfectSquare(int x)
{
    int s = sqrt(x);
    return (s * s == x);
}
 
// Function to check if a number
// is a Fibonacci number or not
bool isFibonacci(int n)
{
    // If 5*n^2 + 4 or 5*n^2 - 4 or
    // both are perfect square
    return isPerfectSquare(5 * n * n + 4)
           || isPerfectSquare(5 * n * n - 4);
}
 
// Function to check if GCD of composite
// numbers from the array a[] which are
// divisible by k is a Fibonacci number or not
void ifgcdFibonacci(int a[], int n, int k)
{
    vector compositeset;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If array element is composite
        // and divisible by k
        if (isComposite(a[i]) && a[i] % k == 0) {
            compositeset.push_back(a[i]);
        }
    }
    int gcd = compositeset[0];
 
    // Calculate GCD of all elements in compositeset
    for (int i = 1; i < compositeset.size(); i++) {
        gcd = __gcd(gcd, compositeset[i]);
        if (gcd == 1) {
            break;
        }
    }
 
    // If GCD is Fibonacci
    if (isFibonacci(gcd)) {
        cout << "Yes";
        return;
    }
    cout << "No";
    return;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 34, 2, 4, 8, 5, 7, 11 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
 
    ifgcdFibonacci(arr, n, k);
    return 0;
}


Java
// Java Program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if a
// number is composite or not
static boolean isComposite(int n)
{
     
    // Corner cases
    if (n <= 1)
        return false;
 
    if (n <= 3)
        return false;
 
    // Check if the number is
    // divisible by 2 or 3 or not
    if (n % 2 == 0 || n % 3 == 0)
        return true;
 
    // Check if n is a multiple of
    // any other prime number
    for(int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return true;
 
    return false;
}
 
// Function to check if a number
// is a Perfect Square or not
static boolean isPerfectSquare(int x)
{
    int s = (int)Math.sqrt(x);
    return (s * s == x);
}
 
// Function to check if a number
// is a Fibonacci number or not
static boolean isFibonacci(int n)
{
     
    // If 5*n^2 + 4 or 5*n^2 - 4 or
    // both are perfect square
    return isPerfectSquare(5 * n * n + 4) ||
           isPerfectSquare(5 * n * n - 4);
}
 
// Function to check if GCD of composite
// numbers from the array a[] which are
// divisible by k is a Fibonacci number or not
static void ifgcdFibonacci(int a[], int n, int k)
{
    Vector compositeset = new Vector<>();
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If array element is composite
        // and divisible by k
        if (isComposite(a[i]) && a[i] % k == 0)
        {
            compositeset.add(a[i]);
        }
    }
    int gcd = compositeset.get(0);
 
    // Calculate GCD of all elements in compositeset
    for(int i = 1; i < compositeset.size(); i++)
    {
        gcd = __gcd(gcd, compositeset.get(i));
         
        if (gcd == 1)
        {
            break;
        }
    }
 
    // If GCD is Fibonacci
    if (isFibonacci(gcd))
    {
        System.out.print("Yes");
        return;
    }
    System.out.print("No");
    return;
}
 
// Recursive function to return gcd of a and b 
static int __gcd(int a, int b) 
{ 
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 34, 2, 4, 8, 5, 7, 11 };
    int n = arr.length;
    int k = 2;
 
    ifgcdFibonacci(arr, n, k);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
import math
 
# Function to check if a
# number is composite or not
def isComposite(n):
     
    # Corner cases
    if n <= 1:
        return False
 
    if n <= 3:
        return False
 
    # Check if the number is
    # divisible by 2 or 3 or not
    if n % 2 == 0 or n % 3 == 0:
        return True
 
    # Check if n is a multiple of
    # any other prime number
    i = 5
    while i * i <= n:
        if ((n % i == 0 ) or
            (n % (i + 2) == 0)):
            return True
             
        i += 6   
 
    return False
 
# Function to check if a number
# is a Perfect Square or not
def isPerfectSquare(x):
     
    s = int(math.sqrt(x))
    return (s * s == x)
     
# Function to check if a number
# is a Fibonacci number or not
def isFibonacci(n):
     
    # If 5*n^2 + 4 or 5*n^2 - 4 or
    # both are perfect square
    return (isPerfectSquare(5 * n * n + 4) or
            isPerfectSquare(5 * n * n - 4))
 
# Function to check if GCD of composite
# numbers from the array a[] which are
# divisible by k is a Fibonacci number or not
def ifgcdFibonacci(a,  n,  k):
 
    compositeset = []
 
    # Traverse the array
    for i in range(n):
 
        # If array element is composite
        # and divisible by k
        if (isComposite(a[i]) and a[i] % k == 0):
            compositeset.append(a[i])
     
    gcd = compositeset[0]
 
    # Calculate GCD of all elements in compositeset
    for i in range(1, len(compositeset), 1):
        gcd = math.gcd(gcd, compositeset[i])
         
        if gcd == 1:
            break
     
    # If GCD is Fibonacci
    if (isFibonacci(gcd)):
        print("Yes")
        return
     
    print("No")
    return
 
# Driver Code
if __name__ == "__main__" :
     
    arr = [ 34, 2, 4, 8, 5, 7, 11 ]
    n = len(arr)
    k = 2
     
    ifgcdFibonacci(arr, n, k)
     
# This code is contributed by jana_sayantan


C#
// C# Program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check if a
// number is composite or not
static bool isComposite(int n)
{
     
    // Corner cases
    if (n <= 1)
        return false;
 
    if (n <= 3)
        return false;
 
    // Check if the number is
    // divisible by 2 or 3 or not
    if (n % 2 == 0 || n % 3 == 0)
        return true;
 
    // Check if n is a multiple of
    // any other prime number
    for(int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return true;
 
    return false;
}
 
// Function to check if a number
// is a Perfect Square or not
static bool isPerfectSquare(int x)
{
    int s = (int)Math.Sqrt(x);
    return (s * s == x);
}
 
// Function to check if a number
// is a Fibonacci number or not
static bool isFibonacci(int n)
{
     
    // If 5*n^2 + 4 or 5*n^2 - 4 or
    // both are perfect square
    return isPerfectSquare(5 * n * n + 4) ||
           isPerfectSquare(5 * n * n - 4);
}
 
// Function to check if GCD of composite
// numbers from the array []a which are
// divisible by k is a Fibonacci number or not
static void ifgcdFibonacci(int []a, int n, int k)
{
    List compositeset = new List();
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If array element is composite
        // and divisible by k
        if (isComposite(a[i]) && a[i] % k == 0)
        {
            compositeset.Add(a[i]);
        }
    }
    int gcd = compositeset[0];
 
    // Calculate GCD of all elements in compositeset
    for(int i = 1; i < compositeset.Count; i++)
    {
        gcd = __gcd(gcd, compositeset[i]);
         
        if (gcd == 1)
        {
            break;
        }
    }
 
    // If GCD is Fibonacci
    if (isFibonacci(gcd))
    {
        Console.Write("Yes");
        return;
    }
    Console.Write("No");
    return;
}
 
// Recursive function to return gcd of a and b 
static int __gcd(int a, int b) 
{ 
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 34, 2, 4, 8, 5, 7, 11 };
    int n = arr.Length;
    int k = 2;
 
    ifgcdFibonacci(arr, n, k);
}
}
 
// This code is contributed by 29AjayKumar


输出:
Yes

时间复杂度: O(N * log(N)),其中N是数组的大小
辅助空间: O(N)