📜  数组的不同素数

📅  最后修改于: 2021-04-24 04:00:14             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是找到给定数组中所有数字的不同素数。
例子:

天真的方法:解决此问题的一种简单方法是找到数组中每个数字的素因。然后在这些素数因子之间找到不同的素数。
时间复杂度: O(N 2 )
高效的方法:一种有效的方法是首先使用Eratosthenes筛子找到所有达到给定极限的质数,然后将它们存储在数组中。对于质数数组中的每个质数,请检查输入数组中的任何数是否可整。如果可以将其整除,则将该素数存储在答案数组中。最后,对给定输入数组中的所有数字重复此过程后,返回答案数组。
下面是上述方法的实现:

CPP
#include 
 
using namespace std;
 
//cppimplementation of the above approach
 
//Function to return an array
//of prime numbers upto n
//using Sieve of Eratosthenes
vector seive(int n){
    vector prime (n + 1,0);
    int p = 2;
    while(p * p<= n){
        if(prime[p]== 0){
            for (int i=2*p;i allPrimes;
    for (int i =2;i distPrime(vector arr, vector allPrimes){
 
    //Creating an empty array
    //to store distinct prime factors
    vector list1;
 
    //Iterating through all the
    //prime numbers and check if
    //any of the prime numbers is a
    //factor of the given input array
    for (int i : allPrimes){
        for (int j :arr){
            if(j % i == 0){
                list1.push_back(i);
                break;
              }
            }
          }
    return list1;
  }
 
//Driver code
 
int main()
{
  //Finding prime numbers upto 10000
  //using Sieve of Eratosthenes
  vector allPrimes = seive(10000);
 
  vector arr = {15, 30, 60};
  vector ans = distPrime(arr, allPrimes);
  cout<<"[";
  for(int i:ans) cout<


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG
{
 
// Function to return an array
// of prime numbers upto n
// using Sieve of Eratosthenes
static ArrayList seive(int n){
    ArrayList prime = new ArrayList();
    for(int i = 0; i < n + 1; i++)
    prime.add(0);
    int p = 2;
    while(p * p <= n){
        if(prime.get(p) == 0){
            for (int i = 2 * p; i < n + 1; i += p)
                prime.set(i, 1);
            }
        p += 1;
    }
 
    ArrayList allPrimes = new ArrayList();
    for (int i = 2; i < n; i++){
    if (prime.get(i) == 0)
        allPrimes.add(i);
    }
    return allPrimes;
}
 
// Function to return distinct
// prime factors from the given array
static ArrayList distPrime(ArrayList arr,
                            ArrayList allPrimes){
 
    // Creating an empty array
    // to store distinct prime factors
    ArrayList list1 = new ArrayList();
 
    // Iterating through all the
    // prime numbers and check if
    // any of the prime numbers is a
    // factor of the given input array
    for (int i = 0; i < allPrimes.size(); i++){
        for (int j = 0; j < arr.size(); j++){
            if(arr.get(j) % allPrimes.get(i) == 0){
                list1.add(allPrimes.get(i));
                break;
            }
            }
        }
    return list1;
}
 
// Driver code
public static void main(String args[])
{
     
    // Finding prime numbers upto 10000
    // using Sieve of Eratosthenes
    ArrayList allPrimes = new ArrayList(seive(10000));
    ArrayList arr = new ArrayList();
    arr.add(15);
    arr.add(30);
    arr.add(60);
    ArrayList ans = new ArrayList(distPrime(arr, allPrimes));
    System.out.print("[");
    for(int i = 0; i < ans.size(); i++)
    System.out.print(ans.get(i) + " ");
    System.out.print("]");
}
}
 
// This code is contributed by Surendra_Gangwar


Python3
# Python3 implementation of the above approach
 
# Function to return an array
# of prime numbers upto n
# using Sieve of Eratosthenes
def seive(n):
    prime =[True]*(n + 1)
    p = 2
    while(p * p<= n):
        if(prime[p] == True):
            for i in range(p * p, n + 1, p):
                prime[i] = False
        p += 1
    allPrimes = [x for x in range(2, n)if prime[x]]
    return allPrimes
 
# Function to return distinct
# prime factors from the given array
def distPrime(arr, allPrimes):
 
    # Creating an empty array
    # to store distinct prime factors
    list1 = list()
     
    # Iterating through all the
    # prime numbers and check if
    # any of the prime numbers is a
    # factor of the given input array
    for i in allPrimes:
        for j in arr:
            if(j % i == 0):
                list1.append(i)
                break
    return list1
 
# Driver code
if __name__=="__main__":
 
    # Finding prime numbers upto 10000
    # using Sieve of Eratosthenes
    allPrimes = seive(10000)
 
    arr = [15, 30, 60]
    ans = distPrime(arr, allPrimes)
    print(ans)
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
// Function to return an array
// of prime numbers upto n
// using Sieve of Eratosthenes
static List seive(int n)
{
    List prime = new List();
    for(int i = 0; i < n + 1; i++)
    prime.Add(0);
    int p = 2;
    while(p * p <= n)
    {
        if(prime[p] == 0)
        {
            for (int i = 2 * p; i < n + 1; i += p)
                prime[i]= 1;
            }
        p += 1;
    }
 
    List allPrimes = new List();
    for (int i = 2; i < n; i++){
    if (prime[i] == 0)
        allPrimes.Add(i);
    }
    return allPrimes;
}
 
// Function to return distinct
// prime factors from the given array
static List distPrime(List arr,
                            List allPrimes){
 
    // Creating an empty array
    // to store distinct prime factors
       List list1 = new List();
 
    // Iterating through all the
    // prime numbers and check if
    // any of the prime numbers is a
    // factor of the given input array
    for (int i = 0; i < allPrimes.Count; i++){
        for (int j = 0; j < arr.Count; j++){
            if(arr[j] % allPrimes[i] == 0){
                list1.Add(allPrimes[i]);
                break;
            }
            }
        }
    return list1;
}
 
// Driver code
public static void Main(string []args)
{
     
    // Finding prime numbers upto 10000
    // using Sieve of Eratosthenes
    List allPrimes = new List(seive(10000));
    List arr = new List();
    arr.Add(15);
    arr.Add(30);
    arr.Add(60);
    List ans = new List(distPrime(arr, allPrimes));
    Console.Write("[");
    for(int i = 0; i < ans.Count; i++)
    Console.Write(ans[i] + " ");
    Console.Write("]");
}
}
 
// This code is contributed by chitranayal


输出:
[2, 3, 5]