📌  相关文章
📜  检查数组元素的LCM是否可被质数整除

📅  最后修改于: 2021-05-07 00:03:23             🧑  作者: Mango

给定一个数组和一个数字k,任务是确定数组的LCM是否可被k整除。
例子 :

Input : int[] a = {10, 20, 15, 25}
              k = 3
Output : true

Input : int[] a = {24, 21, 45, 57, 36};
              k = 23;
Output : false

一个简单的解决方案是首先找到数组元素的LCM,然后检查LCM是否可被k整除。
在这里,在不计算数字的LCM的情况下,我们可以发现数字数组的LCM是否可被质数k整除。如果数组的任何数字都可以被质数k整除,那么该数的LCM也可以被质数k整除。

C++
// C++ program to find LCM of
// array of number is divisible
// by a prime number k or not
#include
using namespace std;
 
// Function to check any number of
// array is divisible by k or not
bool func(int a[], int k, int n)
{
    // If any array element is divisible
    // by k, then LCM of whole array
    // should also be divisible.
    for (int i = 0; i < n; i++)
        if (a[i] % k == 0)
        return true;
    return false;
}
 
// Driver Code
int main()
{
    int a[] = {14, 27, 38, 76, 84};
    int k = 19;
    bool res = func(a, k, 5);
    if(res)
    cout<<"true";
    else
    cout<<"false";
    return 0;
}
 
// This code is contributed
// by Mr. Somesh Awasthi


Java
// Java program to find LCM of
// array of number is divisible
// by a prime number k or not
import java.lang.*;
import java.util.*;
 
class GFG
{
    // Function to check any number
    // of array is divisible by k or not
    static boolean func( int a[], int k)
    {
        // If any array element is divisible
        // by k, then LCM of whole array
        // should also be divisible.
        for (int i = 0; i < a.length; i++)
            if (a[i] % k == 0)
            return true;
        return false;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int[] a = {14, 27, 38, 76, 84};
        int k = 19;
        boolean res = func(a, k);
        System.out.println(res);
    }
}


Python 3
# Python 3 program to find LCM
# of array of number is divisible
# by a prime number k or not
 
 
# Function to check any number of
# array is divisible by k or not
def func( a, k, n) :
     
    # If any array element is
    # divisible by k, then LCM
    # of whole array should also
    # be divisible.
    for i in range(0, n) :
        if ( a[i] % k == 0):
            return True
 
 
# Driver Code
a = [14, 27, 38, 76, 84]
k = 19
res = func(a, k, 5)
 
if(res) :
    print("true")
else :
    print("false")
     
 
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# program to find LCM of array
// of number is divisible by a prime
// number k or not
using System;
 
class GFG
{
     
    // Function to check any number of
    // array is divisible by k or not
    static bool func(int []a, int k)
    {
         
        // If any array element is
        // divisible by k, then LCM
        // of whole array should also
        // be divisible.
        for (int i = 0; i < a.Length; i++)
            if (a[i] % k == 0)
            return true;
        return false;
    }
 
    // Driver code
    public static void Main()
    {
        int []a = {14, 27, 38, 76, 84};
        int k = 19;
        bool res = func(a, k);
        Console.Write(res);
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


输出 :

true