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

📅  最后修改于: 2021-09-03 03:39:32             🧑  作者: Mango

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

例子:

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

  1. 创建一个函数isComposite()来检查一个数字是否是合数。
  2. 创建另一个函数isFibonacci()来检查数字是否为斐波那契数。
  3. 初始化一个整数向量,比如复合集,以及另一个整数变量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


Javascript


输出:
Yes

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live