📜  计算除数为素数的数组元素

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

给定一个由N 个正整数组成的数组arr[] ,任务是找到除数为素数的数组元素的数量。

例子:

朴素方法:解决给定问题的最简单方法是找到每个数组元素的除数数并检查除数数是否为素数。如果发现为真,则增加count 。否则,检查下一个元素。检查所有数组元素后,打印获得的计数
时间复杂度: O(N (3/2) )
辅助空间: O(1)

有效方法:可以通过使用埃拉托色尼筛法预先计算素数来优化上述方法。请按照以下步骤解决给定的问题:

  • 初始化一个变量,比如count ,以存储除数为素数的数组元素的计数。
  • 使用 Eratosthenes 筛将所有素数存储在一个布尔数组中,直到数组的最大元素,比如prime[]
  • 现在找到元素的除数,直到数组的最大元素并将它们存储在一个数组中,比如countDivisor[]
  • 遍历给定的数组arr[]和每个元素arr[i] ,如果countDivisor[arr[i]] 的值为素数,则将计数增加1 。否则,检查下一个元素。
  • 完成以上步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the array elements
// whose count of divisors is prime
int primeDivisors(int arr[], int N)
{
    // Stores the maximum element
    int K = arr[0];
 
    // Find the maximum element
    for (int i = 1; i < N; i++) {
        K = max(K, arr[i]);
    }
 
    // Store if i-th element is
    // prime(0) or non-prime(1)
    int prime[K + 1] = { 0 };
 
    // Base Case
    prime[0] = 1;
    prime[1] = 1;
 
    for (int i = 2; i < K + 1; i++) {
 
        // If i is a prime number
        if (!prime[i]) {
 
            // Mark all multiples
            // of i as non-prime
            for (int j = 2 * i;
                 j < K + 1; j += i) {
 
                prime[j] = 1;
            }
        }
    }
 
    // Stores the count of divisors
    int factor[K + 1] = { 0 };
 
    // Base Case
    factor[0] = 0;
    factor[1] = 1;
 
    for (int i = 2; i < K + 1; i++) {
 
        factor[i] += 1;
 
        // Iterate to count factors
        for (int j = i; j < K + 1;
             j += i) {
 
            factor[j] += 1;
        }
    }
 
    // Stores the count of array
    // elements whose count of
    // divisors is a prime number
    int count = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If count of divisors is prime
        if (prime[factor[arr[i]]] == 0)
            count++;
    }
 
    // Return the resultant count
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 13, 17, 25 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << primeDivisors(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
   
    // Function to count the array elements
    // whose count of divisors is prime
    public static int primeDivisors(int[] arr, int N)
    {
       
        // Stores the maximum element
        int K = arr[0];
 
        // Find the maximum element
        for (int i = 1; i < N; i++) {
            K = Math.max(K, arr[i]);
        }
 
        // Store if i-th element is
        // prime(0) or non-prime(1)
        int[] prime = new int[K + 1];
 
        // Base Case
        prime[0] = 1;
        prime[1] = 1;
 
        for (int i = 2; i < K + 1; i++) {
 
            // If i is a prime number
            if (prime[i] == 0) {
 
                // Mark all multiples
                // of i as non-prime
                for (int j = 2 * i; j < K + 1; j += i) {
 
                    prime[j] = 1;
                }
            }
        }
 
        // Stores the count of divisors
        int[] factor = new int[K + 1];
 
        // Base Case
        factor[0] = 0;
        factor[1] = 1;
 
        for (int i = 2; i < K + 1; i++) {
 
            factor[i] += 1;
 
            // Iterate to count factors
            for (int j = i; j < K + 1; j += i) {
 
                factor[j] += 1;
            }
        }
 
        // Stores the count of array
        // elements whose count of
        // divisors is a prime number
        int count = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < N; i++) {
 
            // If count of divisors is prime
            if (prime[factor[arr[i]]] == 0)
                count++;
        }
 
        // Return the resultant count
        return count;
    }
 
    // Driver Code
    public static void main(String args[]) {
      int[] arr = { 10, 13, 17, 25 };
        int N = arr.length;
       System.out.println(primeDivisors(arr, N));
 
    }
}
 
// This code is contributed by SoumikMondal.


Python3
# Python3 program for the above approach
 
# Function to count the array elements
# whose count of divisors is prime
def primeDivisors(arr, N):
     
    # Stores the maximum element
    K = arr[0]
 
    # Find the maximum element
    for i in range(1, N):
        K = max(K, arr[i])
 
    # Store if i-th element is
    # prime(0) or non-prime(1)
    prime = [0] * (K + 1)
     
    # Base Case
    prime[0] = 1
    prime[1] = 1
 
    for i in range(2, K + 1):
         
        # If i is a prime number
        if (not prime[i]):
             
            # Mark all multiples
            # of i as non-prime
            for j in range(2 * i, K + 1, i):
                prime[j] = 1
 
    # Stores the count of divisors
    factor = [0] * (K + 1)
     
    # Base Case
    factor[0] = 0
    factor[1] = 1
 
    for i in range(2, K + 1):
        factor[i] += 1
 
        # Iterate to count factors
        for j in range(i, K + 1, i):
            factor[j] += 1
 
    # Stores the count of array
    # elements whose count of
    # divisors is a prime number
    count = 0
 
    # Traverse the array arr[]
    for i in range(N):
         
        # If count of divisors is prime
        if (prime[factor[arr[i]]] == 0):
            count += 1
 
    # Return the resultant count
    return count
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 10, 13, 17, 25 ]
    N = len(arr)
     
    print (primeDivisors(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Function to count the array elements
    // whose count of divisors is prime
    static int primeDivisors(int[] arr, int N)
    {
       
        // Stores the maximum element
        int K = arr[0];
 
        // Find the maximum element
        for (int i = 1; i < N; i++) {
            K = Math.Max(K, arr[i]);
        }
 
        // Store if i-th element is
        // prime(0) or non-prime(1)
        int[] prime = new int[K + 1];
 
        // Base Case
        prime[0] = 1;
        prime[1] = 1;
 
        for (int i = 2; i < K + 1; i++) {
 
            // If i is a prime number
            if (prime[i] == 0) {
 
                // Mark all multiples
                // of i as non-prime
                for (int j = 2 * i; j < K + 1; j += i) {
 
                    prime[j] = 1;
                }
            }
        }
 
        // Stores the count of divisors
        int[] factor = new int[K + 1];
 
        // Base Case
        factor[0] = 0;
        factor[1] = 1;
 
        for (int i = 2; i < K + 1; i++) {
 
            factor[i] += 1;
 
            // Iterate to count factors
            for (int j = i; j < K + 1; j += i) {
 
                factor[j] += 1;
            }
        }
 
        // Stores the count of array
        // elements whose count of
        // divisors is a prime number
        int count = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < N; i++) {
 
            // If count of divisors is prime
            if (prime[factor[arr[i]]] == 0)
                count++;
        }
 
        // Return the resultant count
        return count;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 10, 13, 17, 25 };
        int N = arr.Length;
        Console.WriteLine(primeDivisors(arr, N));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
3

时间复杂度: O(M*log M),其中M给定数组的最大元素
辅助空间: O(M)

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