📜  N个数的常见除数

📅  最后修改于: 2021-04-21 21:13:35             🧑  作者: Mango

给定N个整数的数组arr [] 。任务是找到所有N个整数的所有公共除数。

例子

方法:

  1. 要查找所有的N个整数的公约数的给定阵列ARR []发现在所有ARR整数的最大公约数(GCD)[]。
  2. 使用本文中讨论的方法,找到在上述步骤中获得的所有最大公约数(gcd)的所有约数。

下面是上述方法的实现:

C++
// C++ program to find all common
// divisors of N numbers
#include 
using namespace std;
  
// Function to calculate gcd of
// two numbers
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
// Function to print all the
// common divisors
void printAllDivisors(int arr[], int N)
{
    // Variable to find the gcd
    // of N numbers
    int g = arr[0];
  
    // Set to store all the
    // common divisors
    set divisors;
  
    // Finding GCD of the given
    // N numbers
    for (int i = 1; i < N; i++) {
        g = gcd(arr[i], g);
    }
  
    // Finding divisors of the
    // HCF of n numbers
    for (int i = 1; i * i <= g; i++) {
        if (g % i == 0) {
            divisors.insert(i);
            if (g / i != i)
                divisors.insert(g / i);
        }
    }
  
    // Print all the divisors
    for (auto& it : divisors)
        cout << it << " ";
}
  
// Driver's Code
int main()
{
    int arr[] = { 6, 90, 12, 18, 30, 24 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function to print all the
    // common divisors
    printAllDivisors(arr, n);
    return 0;
}


Java
// Java program to find all common
// divisors of N numbers
import java.util.*;
  
class GFG
{
  
// Function to calculate gcd of
// two numbers
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
// Function to print all the
// common divisors
static void printAllDivisors(int arr[], int N)
{
    // Variable to find the gcd
    // of N numbers
    int g = arr[0];
  
    // Set to store all the
    // common divisors
    HashSet divisors = new HashSet();
  
    // Finding GCD of the given
    // N numbers
    for (int i = 1; i < N; i++) 
    {
        g = gcd(arr[i], g);
    }
  
    // Finding divisors of the
    // HCF of n numbers
    for (int i = 1; i * i <= g; i++)
    {
        if (g % i == 0) 
        {
            divisors.add(i);
            if (g / i != i)
                divisors.add(g / i);
        }
    }
  
    // Print all the divisors
    for (int it : divisors)
        System.out.print(it+ " ");
}
  
// Driver's Code
public static void main(String[] args)
{
    int arr[] = { 6, 90, 12, 18, 30, 24 };
    int n = arr.length;
  
    // Function to print all the
    // common divisors
    printAllDivisors(arr, n);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find all common
# divisors of N numbers
  
# Function to calculate gcd of
# two numbers
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)
  
# Function to prall the
# common divisors
def printAllDivisors(arr, N):
      
    # Variable to find the gcd
    # of N numbers
    g = arr[0]
  
    # Set to store all the
    # common divisors
    divisors = dict()
  
    # Finding GCD of the given
    # N numbers
    for i in range(1, N):
        g = gcd(arr[i], g)
  
    # Finding divisors of the
    # HCF of n numbers
    for i in range(1, g + 1):
        if i*i > g:
            break
        if (g % i == 0):
            divisors[i] = 1
            if (g // i != i):
                divisors[g // i] = 1
  
    # Prall the divisors
    for it in sorted(divisors):
        print(it, end=" ")
  
# Driver's Code
if __name__ == '__main__':
    arr= [6, 90, 12, 18, 30, 24]
    n = len(arr)
  
    # Function to prall the
    # common divisors
    printAllDivisors(arr, n)
  
# This code is contributed by mohit kumar 29


C#
// C# program to find all common
// divisors of N numbers
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function to calculate gcd of
// two numbers
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
// Function to print all the
// common divisors
static void printAllDivisors(int []arr, int N)
{
    // Variable to find the gcd
    // of N numbers
    int g = arr[0];
  
    // Set to store all the
    // common divisors
    HashSet divisors = new HashSet();
  
    // Finding GCD of the given
    // N numbers
    for (int i = 1; i < N; i++) 
    {
        g = gcd(arr[i], g);
    }
  
    // Finding divisors of the
    // HCF of n numbers
    for (int i = 1; i * i <= g; i++)
    {
        if (g % i == 0) 
        {
            divisors.Add(i);
            if (g / i != i)
                divisors.Add(g / i);
        }
    }
  
    // Print all the divisors
    foreach (int it in divisors)
        Console.Write(it+ " ");
}
  
// Driver's Code
public static void Main(String[] args)
{
    int []arr = { 6, 90, 12, 18, 30, 24 };
    int n = arr.Length;
  
    // Function to print all the
    // common divisors
    printAllDivisors(arr, n);
}
}
  
// This code is contributed by PrinciRaj1992


输出:
1 2 3 6

时间复杂度: O(N * log(M)),其中N是给定数组的长度,M是数组中的最大元素。