📜  非重复素数

📅  最后修改于: 2021-09-07 02:51:28             🧑  作者: Mango

给定一个包含重复素数和非素数的数组arr[] ,任务是找到只出现一次的素数。

例子:

朴素的方法:要解决上述问题,解决方案是检查每个元素是否为素数。如果它是素数,则检查它是否只出现一次。一旦找到出现一次的素数元素,就打印它。

时间复杂度: O(N 2 )

高效的方法:可以使用 Sieve 和 Hashing 算法进一步优化上述方法。

  1. 使用 Sieve 在哈希表中预先计算和存储素数。
  2. 还要创建一个 HashMap 来存储数字及其频率。
  3. 一一遍历数组中的所有元素,并:
    • 使用 O(1) 中的 Sieve Hash Table 检查当前数字是否为质数。
    • 如果数字是素数,则增加其在 HashMap 中的频率。
  4. 遍历HashMap,打印所有频率为1的数字。

下面是上述方法的实现:

Java
// Java program to find
// Non-repeating Primes
 
import java.util.*;
 
class GFG {
 
    // Function to find count of prime
    static Vector findPrimes(
        int arr[], int n)
    {
        // Find maximum value in the array
        int max_val = Arrays
                          .stream(arr)
                          .max()
                          .getAsInt();
 
        // Find and store all prime numbers
        // up to max_val using Sieve
 
        // Create a boolean array "prime[0..n]".
        // A value in prime[i]
        // will finally be false
        // if i is Not a prime, else true.
        Vector prime
            = new Vector<>(max_val + 1);
 
        for (int i = 0; i < max_val + 1; i++)
            prime.add(i, Boolean.TRUE);
 
        // Remaining part of SIEVE
        prime.add(0, Boolean.FALSE);
        prime.add(1, Boolean.FALSE);
        for (int p = 2; p * p <= max_val; p++) {
 
            // If prime[p] is not changed,
            // then it is a prime
            if (prime.get(p) == true) {
 
                // Update all multiples of p
                for (int i = p * 2;
                     i <= max_val;
                     i += p)
                    prime.add(i, Boolean.FALSE);
            }
        }
 
        return prime;
    }
 
    // Function to print
    // Non-repeating primes
    static void nonRepeatingPrimes(
        int arr[], int n)
    {
 
        // Precompute primes using Sieve
        Vector prime
            = findPrimes(arr, n);
 
        // Create HashMap to store
        // frequency of prime numbers
        HashMap mp
            = new HashMap<>();
 
        // Traverse through array elements and
        // Count frequencies of all primes
        for (int i = 0; i < n; i++) {
            if (prime.get(arr[i]))
                if (mp.containsKey(arr[i]))
                    mp.put(arr[i],
                           mp.get(arr[i]) + 1);
                else
                    mp.put(arr[i], 1);
        }
 
        // Traverse through map and
        // print non repeating primes
        for (Map.Entry
                 entry : mp.entrySet()) {
            if (entry.getValue() == 1)
                System.out.println(
                    entry.getKey());
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 3, 4, 6, 7, 9,
                      7, 23, 21, 3 };
        int n = arr.length;
 
        nonRepeatingPrimes(arr, n);
    }
}


Python3
# Python3 program to find
# Non-repeating Primes
 
# Function to find count of prime
def findPrimes( arr, n):
 
    # Find maximum value in the array
    max_val =  max(arr)
     
    # Find and store all prime numbers
    # up to max_val using Sieve
    # Create a boolean array "prime[0..n]".
    # A value in prime[i]
    # will finally be false
    # if i is Not a prime, else true.
    prime = [True for i in range(max_val + 1)]
 
    # Remaining part of SIEVE
    prime[0] = False
    prime[1] = False
    p = 2
    while(p * p <= max_val):
 
        # If prime[p] is not changed,
        # then it is a prime
        if (prime[p] == True):
           
            # Update all multiples of p
            for i in range(p * 2, max_val + 1, p): 
                prime[i] = False
        p += 1     
    return prime;
 
# Function to print
# Non-repeating primes
def nonRepeatingPrimes(arr, n):
 
    # Precompute primes using Sieve
    prime = findPrimes(arr, n);
     
    # Create HashMap to store
    # frequency of prime numbers
    mp = dict()
 
    # Traverse through array elements and
    # Count frequencies of all primes
    for i in range(n):  
        if (prime[arr[i]]):
            if (arr[i] in mp):
                mp[arr[i]] += 1
            else:
                mp[arr[i]] = 1
     
    # Traverse through map and
    # print non repeating primes
    for entry in mp.keys():
        if (mp[entry] == 1):
            print(entry);
     
# Driver code
if __name__ == '__main__':
    arr = [ 2, 3, 4, 6, 7, 9, 7, 23, 21, 3]
    n = len(arr)
    nonRepeatingPrimes(arr, n);
 
# This code is contributed by pratham76.


C#
// C# program to find
// Non-repeating Primes
using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
 
class GFG{
 
// Function to find count of prime
static List findPrimes(int []arr, int n)
{
     
    // Find maximum value in the array
    int max_val = arr.Max();
 
    // Find and store all prime numbers
    // up to max_val using Sieve
 
    // Create a boolean array "prime[0..n]".
    // A value in prime[i]
    // will finally be false
    // if i is Not a prime, else true.
    List prime = new List(max_val + 1);
 
    for(int i = 0; i < max_val + 1; i++)
        prime.Add(true);
 
    // Remaining part of SIEVE
    prime[0] = false;
    prime[1] = false;
     
    for(int p = 2; p * p <= max_val; p++)
    {
         
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true)
        {
             
            // Update all multiples of p
            for(int i = p * 2;
                    i <= max_val;
                    i += p)
                prime[i] = false;
        }
    }
    return prime;
}
 
// Function to print
// Non-repeating primes
static void nonRepeatingPrimes(int []arr, int n)
{
     
    // Precompute primes using Sieve
    List prime = findPrimes(arr, n);
 
    // Create HashMap to store
    // frequency of prime numbers
    Dictionary mp = new Dictionary();
 
    // Traverse through array elements and
    // Count frequencies of all primes
    for(int i = 0; i < n; i++)
    {
        if (prime[arr[i]])
            if (mp.ContainsKey(arr[i]))
                mp[arr[i]]++;
            else
                mp.Add(arr[i], 1);
    }
 
    // Traverse through map and
    // print non repeating primes
    foreach(KeyValuePair entry in mp)
    {
        if (entry.Value == 1)
            Console.WriteLine(entry.Key);
    }
}
 
// Driver code
public static void Main(string[] args)
{
    int []arr = { 2, 3, 4, 6, 7, 9,
                  7, 23, 21, 3 };
    int n = arr.Length;
 
    nonRepeatingPrimes(arr, n);
}
}
 
// This code is contributed by rutvik_56


输出:
2
23

时间复杂度: O(O(n*log(log(n))))
辅助空间: O(K),其中 K 是数组中的最大值。

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