📜  xor赋予唯一素数的所有对

📅  最后修改于: 2021-04-22 09:32:12             🧑  作者: Mango

给定数组arr [] ,任务是计算其xor给出唯一素数的所有对,即没有两个对应该给出相同素数。
例子:

方法:迭代每个可能的对,并检查当前对的xor是否为质数。如果它是素数,则更新count = count + 1并将素数保存在unordered_map中,以便跟踪重复的素数。最后打印计数
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function that returns true if n is prime
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}
 
// Function to return the count of valid pairs
int countPairs(int a[], int n)
{
    int count = 0;
 
    unordered_map m;
 
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // If xor(a[i], a[j]) is prime and unique
            if (isPrime(a[i] ^ a[j]) && m[a[i] ^ a[j]] == 0) {
                m[(a[i] ^ a[j])]++;
                count++;
            }
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int a[] = { 10, 12, 23, 45, 5, 6 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << countPairs(a, n);
}


Java
// Java implementation of above approach
import java.util.*;
class solution
{
  
// Function that returns true if n is prime
static boolean isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
  
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
  
    return true;
}
  
// Function to return the count of valid pairs
static int countPairs(int a[], int n)
{
    int count = 0;
  
    Map m=new HashMap< Integer,Integer>();
  
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
  
            // If xor(a[i], a[j]) is prime and unique
            if (isPrime(a[i] ^ a[j]) && m.get(a[i] ^ a[j]) == null) {
                m.put((a[i] ^ a[j]),1);
                count++;
            }
        }
    }
  
    return count;
}
  
// Driver code
public static void main(String args[])
{
    int a[] = { 10, 12, 23, 45, 5, 6 };
  
    int n = a.length;
  
    System.out.println(countPairs(a, n));
}
}
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of above approach
 
# Function that returns true if n is prime
def isPrime(n):
     
    # Corner cases
    if n <= 1:
        return False
    if n <= 3:
        return True
 
    # This is checked so that we can skip
    # middle five numbers in below loop
    if n % 2 == 0 or n % 3 == 0:
        return False
     
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
         
    return True
 
# Function to return the count of valid pairs
def countPairs(a, n):
    count = 0
 
    m = dict()
 
    for i in range(n - 1):
        for j in range(i + 1, n):
 
            # If xor(a[i], a[j]) is prime and unique
            if isPrime(a[i] ^ a[j]) and m.get(a[i] ^ a[j], 0) == 0:
                m[(a[i] ^ a[j])] = 1
                count += 1
                 
    return count
 
# Driver code
a = [10, 12, 23, 45, 5, 6]
 
n = len(a)
 
print(countPairs(a, n))
 
# This code is contributed by
# Rajnis09


C#
// C# implementation of the approach
using System ;
using System.Collections ;
 
class solution
{
 
    // Function that returns true if n is prime
    static bool isPrime(int n)
    {
        // Corner cases
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
     
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
     
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
     
        return true;
    }
     
    // Function to return the count of valid pairs
    static int countPairs(int []a, int n)
    {
        int count = 0;
     
        Hashtable m=new Hashtable();
     
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
     
                // If xor(a[i], a[j]) is prime and unique
                if (isPrime(a[i] ^ a[j]) && m[a[i] ^ a[j]] == null)
                {
                    m.Add((a[i] ^ a[j]),1);
                    count++;
                }
            }
        }
     
        return count;
    }
     
    // Driver code
    public static void Main()
    {
        int []a = { 10, 12, 23, 45, 5, 6 };
     
        int n = a.Length;
     
        Console.WriteLine(countPairs(a, n));
    }
    // This code is contributed by Ryuga
}


Javascript


输出:
4