📌  相关文章
📜  将给定数组的所有元素相除的整数计数

📅  最后修改于: 2021-05-08 17:29:48             🧑  作者: Mango

给定N个元素的数组arr [] 。任务是找到将所有数组元素相除的正整数计数。

例子:

方法:我们知道将划分所有数组元素的最大整数将是数组的gcd,而将划分数组所有元素的所有其他整数将必须是此gcd的因数。因此,有效整数的数量将等于所有数组元素的gcd的因子的数量。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of 
// the required integers
int getCount(int a[], int n)
{
  
    // To store the gcd of the array elements
    int gcd = 0;
    for (int i = 0; i < n; i++)
        gcd = __gcd(gcd, a[i]);
  
    // To store the count of factors
    // of the found gcd
    int cnt = 0;
  
    for (int i = 1; i * i <= gcd; i++) {
        if (gcd % i == 0) {
  
            // If g is a perfect square
            if (i * i == gcd)
                cnt++;
  
            // Factors appear in pairs
            else
                cnt += 2;
        }
    }
  
    return cnt;
}
  
// Driver code
int main()
{
    int a[] = { 4, 16, 1024, 48 };
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << getCount(a, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
    // Recursive function to return gcd 
    static int calgcd(int a, int b) 
    { 
        if (b == 0) 
            return a; 
        return calgcd(b, a % b); 
    }
      
    // Function to return the count of 
    // the required integers
    static int getCount(int [] a, int n)
    {
      
        // To store the gcd of the array elements
        int gcd = 0;
        for (int i = 0; i < n; i++)
            gcd = calgcd(gcd, a[i]);
      
        // To store the count of factors
        // of the found gcd
        int cnt = 0;
      
        for (int i = 1; i * i <= gcd; i++) 
        {
            if (gcd % i == 0) 
            {
      
                // If g is a perfect square
                if (i * i == gcd)
                    cnt++;
      
                // Factors appear in pairs
                else
                    cnt += 2;
            }
        }
        return cnt;
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        int [] a = { 4, 16, 1024, 48 };
        int n = a.length;
      
        System.out.println(getCount(a, n));
    }
}
  
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
  
# Function to return the count of
# the required integers
from math import gcd as __gcd
def getCount(a, n):
  
    # To store the gcd of the array elements
    gcd = 0
    for i in range(n):
        gcd = __gcd(gcd, a[i])
  
    # To store the count of factors
    # of the found gcd
    cnt = 0
  
    for i in range(1, gcd + 1):
        if i * i > gcd:
            break
        if (gcd % i == 0):
  
            # If g is a perfect square
            if (i * i == gcd):
                cnt += 1
  
            # Factors appear in pairs
            else:
                cnt += 2
  
    return cnt
  
# Driver code
a = [4, 16, 1024, 48]
n = len(a)
  
print(getCount(a, n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
    // Recursive function to return gcd 
    static int calgcd(int a, int b) 
    { 
        if (b == 0) 
            return a; 
        return calgcd(b, a % b); 
    }
      
    // Function to return the count of 
    // the required integers
    static int getCount(int [] a, int n)
    {
      
        // To store the gcd of the array elements
        int gcd = 0;
        for (int i = 0; i < n; i++)
            gcd = calgcd(gcd, a[i]);
      
        // To store the count of factors
        // of the found gcd
        int cnt = 0;
      
        for (int i = 1; i * i <= gcd; i++) 
        {
            if (gcd % i == 0) 
            {
      
                // If g is a perfect square
                if (i * i == gcd)
                    cnt++;
      
                // Factors appear in pairs
                else
                    cnt += 2;
            }
        }
        return cnt;
    }
      
    // Driver code
    public static void Main () 
    {
        int [] a = { 4, 16, 1024, 48 };
        int n = a.Length;
      
        Console.WriteLine(getCount(a, n));
    }
}
  
// This code is contributed by ihritik


输出:
3