Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not
Last Updated :
27 Apr, 2021
Given array arr[] consisting of N non-negative integers, and an integer K, the task is to check if GCD of all composite numbers in the array which are divisible by K is a Fibonacci Number or not. IF found to be true, print "Yes". Otherwise, print "No".
Examples:
Input: arr[] = {13, 55, 1331, 7, 13, 11, 44, 77, 144, 89}, K = 11
Output: No
Explanation: Composite Numbers from the array which are divisible by 11 are {55, 1331, 11, 44, 77}. GCD of these elements is equal to 11, which is not a Fibonacci Number.
Input: arr[] = {34, 2, 4, 8, 5, 7, 11}, K = 2
Output:Yes
Explanation: Composite Numbers from the array which are divisible by 2 are {34, 2, 4, 8}. GCD of these elements is equal to 2, which is not a Fibonacci Number.
Approach: Follow the steps below to solve the problem:
- Create a function isComposite() to check if a number is a composite number or not.
- Create another function isFibonacci() to check if a number is Fibonacci number or not.
- Initialize a vector of integers, say compositeset, and another integer variable gcd to store gcd of the composite numbers from the array which are divisible by K.
- Traverse the array arr[].
- For every element arr[i], check if it is composite and divisible by K or not. If found to be true, insert it into the vector compositeset
- Calculate GCD of all the elements in the vector compositeset and store it in the variable gcd.
- Check if gcd is a Fibonacci Number or not.
- If found to be true, then print "Yes". Otherwise, print "No".
Below is the implementation of the above approach:
C++
// C++ Program for the above approach
#include <bits/stdc++.h>
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<int> 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<Integer> 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<int> compositeset = new List<int>();
// 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
<script>
// Javascript Program for the above approach
function __gcd(a, b) {
if (!b) {
return a;
}
return __gcd(b, a % b);
}
// Function to check if a
// number is composite or not
function 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 || n % 3 == 0)
return true;
var i;
// Check if n is a multiple of
// any other prime number
for(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
function isPerfectSquare(x)
{
var s = Math.sqrt(x);
return (s * s == x);
}
// Function to check if a number
// is a Fibonacci number or not
function isFibonacci(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
function ifgcdFibonacci(a, n, k)
{
var compositeset = [];
var i;
// Traverse the array
for (i = 0; i < n; i++) {
// If array element is composite
// and divisible by k
if (isComposite(a[i]) && a[i] % k == 0) {
compositeset.push(a[i]);
}
}
var gcd = compositeset[0];
// Calculate GCD of all elements in compositeset
for (i = 1; i < compositeset.length; i++) {
gcd = __gcd(gcd, compositeset[i]);
if (gcd == 1) {
break;
}
}
// If GCD is Fibonacci
if (isFibonacci(gcd)) {
document.write("Yes");
return;
}
document.write("No");
return;
}
// Driver Code
var arr = [34, 2, 4, 8, 5, 7, 11];
var n = arr.length;
var k = 2;
ifgcdFibonacci(arr, n, k);
</script>
Time Complexity: O(N*log(N)), where N is the size of the array
Auxiliary Space: O(N)
Similar Reads
Check if a number is divisible by all prime divisors of another number Given two positive integers x and y, the task is to determine whether x is divisible by all the prime divisors of y. That means, every prime number that divides y should also divide x. If this condition holds true, print Yes, otherwise print No.Examples: Input: x = 120, y = 75Output: YesExplanation:
11 min read
Check if the number formed by the last digits of N numbers is divisible by 10 or not Given an array arr[] of size N consisting of non-zero positive integers. The task is to determine whether the number that is formed by selecting the last digits of all the numbers is divisible by 10 or not. If the number is divisible by 10, then print Yes otherwise print No.Examples: Input: arr[] =
4 min read
Largest K digit number divisible by all numbers in given array Given an array arr[] of size N and an integer K. The task is to find the largest K digit number divisible by all number of arr[]. Examples: Input: arr[] = {2, 3, 5}, K = 3Output: 990Explanation: 990 is the largest 3 digit number divisible by 2, 3 and 5. Input: arr[] = {91, 93, 95}, K = 3Output: -1Ex
6 min read
Smallest K digit number divisible by all numbers in given array Given an array arr[]. The task is to create the smallest K digit number divisible by all numbers of arr[]. Examples: Input: arr[] = {2, 3, 5}, N = 3Output: 120Explanation: 120 is divisible by 2, 3 and 5 Input: arr[] = {2, 6, 7, 4, 5}, N = 5Output: 10080 Recursive approach: This problem can be solved
7 min read
Check if a M-th fibonacci number divides N-th fibonacci number Given two numbers M and N, the task is to check if the M-th and N-th Fibonacci numbers perfectly divide each other or not.Examples: Input: M = 3, N = 6 Output: Yes F(3) = 2, F(6) = 8 and F(6) % F(3) = 0 Input: M = 2, N = 9 Output: Yes A naive approach will be to find the N-th and M-th Fibonacci numb
8 min read
Sum and Product of all Composite numbers which are divisible by k in an array Given an array arr[] of N positive integers. The task is to find the sum of all composite elements which are divisible by a given number k in the given array. Examples: Input: arr[] = {1, 3, 4, 5, 7}, k = 2 Output: 4, 4 There is one composite number i.e. 4. So, sum = 4 and product = 4 Input: arr[] =
9 min read