📜  非素数与数组素数的XOR的绝对差

📅  最后修改于: 2021-05-04 18:02:36             🧑  作者: Mango

给定N个正整数的数组arr [] ,任务是计算非素数与素数的XOR之间的绝对差。请注意1既不是素数也不是复合数。

例子:

天真的方法:一个简单的解决方案是遍历数组并检查每个元素是否为素数。如果number是质数,则将其与X1表示质XOR,否则将XOR与X2表示非质数XOR 。遍历整个数组后,取X1X2之间的绝对差。

高效的方法:使用Eratosthenes筛子生成所有素数,直到数组的最大元素。现在,遍历数组并检查当前元素是否为质数。如果元素是素数,则将其与X1进行XOR运算,否则将其与X2进行XOR运算。最终打印abs(X1-X2)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the absolute difference
// between the XOR of non-primes and the
// XOR of primes in the given array
int calculateDifference(int arr[], int n)
{
  
    // Find maximum value in the array
    int max_val = *max_element(arr, arr + n);
  
    // USE SIEVE TO FIND ALL PRIME NUMBERS LESS
    // THAN OR EQUAL TO max_val
    // 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(max_val + 1, 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;
        }
    }
  
    // Store the XOR of primes in X1 and
    // the XOR of non primes in X2
    int X1 = 1, X2 = 1;
    for (int i = 0; i < n; i++) {
  
        if (prime[arr[i]]) {
  
            // The number is prime
            X1 ^= arr[i];
        }
        else if (arr[i] != 1) {
  
            // The number is non-prime
            X2 ^= arr[i];
        }
    }
  
    // Return the absolute difference
    return abs(X1 - X2);
}
  
// Driver code
int main()
{
    int arr[] = { 1, 3, 5, 10, 15, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Find the absolute difference
    cout << calculateDifference(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.Arrays;
  
class GFG
{
  
    // Function to return 
    // max_element from an array
    static int max_element(int[] arr) 
    {
        int max = arr[0];
  
        for (int ele : arr)
            if (max < ele)
                max = ele;
  
        return max;
    }
  
    // Function to find the absolute difference
    // between the XOR of non-primes and the
    // XOR of primes in the given array
    static int calculateDifference(int[] arr, int n) 
    {
  
        // Find maximum value in the array
        int max_val = max_element(arr);
  
        // USE SIEVE TO FIND ALL PRIME NUMBERS 
        // LESS THAN OR EQUAL TO max_val
        // Create a boolean array "prime[0..n]". 
        // A value in prime[i] will finally be false
        // if i is Not a prime, else true.
        boolean[] prime = new boolean[max_val + 1];
        Arrays.fill(prime, 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;
            }
        }
  
        // Store the XOR of primes in X1 and
        // the XOR of non primes in X2
        int x1 = 1, x2 = 1;
        for (int i = 0; i < n; i++) 
        {
            if (prime[arr[i]])
  
                // The number is prime
                x1 ^= arr[i];
            else if (arr[i] != 1)
  
                // The number is non-prime
                x2 ^= arr[i];
        }
  
        // Return the absolute difference
        return Math.abs(x1 - x2);
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        int[] arr = { 1, 3, 5, 10, 15, 7 };
        int n = arr.length;
  
        // Find the absolute difference
        System.out.println(calculateDifference(arr, n));
    }
}
  
// This code is contributed by
// sanjeev2552


Python3
# Python3 implementation of the approach
  
# Function to find the absolute difference
# between the XOR of non-primes and the
# XOR of primes in the given array
def calculateDifference(arr, n):
  
    # Find maximum value in the array
    max_val = max(arr)
  
    # USE SIEVE TO FIND ALL PRIME NUMBERS 
    # LESS THAN OR EQUAL TO max_val
    # 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
    for p in range(2, max_val + 1):
  
        if p * p > max_val + 1:
            break
  
        # If prime[p] is not changed, 
        # then it is a prime
        if (prime[p] == True):
  
            # Update all multiples of p
            for i in range(2 * p, max_val + 1, p):
                prime[i] = False
  
    # Store the XOR of primes in X1 and
    # the XOR of non primes in X2
    X1 = 1
    X2 = 1
    for i in range(n):
  
        if (prime[arr[i]]):
  
            # The number is prime
            X1 ^= arr[i]
  
        elif (arr[i] != 1):
  
            # The number is non-prime
            X2 ^= arr[i]
  
    # Return the absolute difference
    return abs(X1 - X2)
  
# Driver code
arr = [1, 3, 5, 10, 15, 7]
n = len(arr)
  
# Find the absolute difference
print(calculateDifference(arr, n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach 
using System;
      
class GFG
{
  
    // Function to return 
    // max_element from an array
    static int max_element(int[] arr) 
    {
        int max = arr[0];
  
        foreach (int ele in arr)
            if (max < ele)
                max = ele;
  
        return max;
    }
  
    // Function to find the absolute difference
    // between the XOR of non-primes and the
    // XOR of primes in the given array
    static int calculateDifference(int[] arr, int n) 
    {
  
        // Find maximum value in the array
        int max_val = max_element(arr);
  
        // USE SIEVE TO FIND ALL PRIME NUMBERS 
        // LESS THAN OR EQUAL TO max_val
        // Create a boolean array "prime[0..n]". 
        // A value in prime[i] will finally be false
        // if i is Not a prime, else true.
        bool[] prime = new bool[max_val + 1];
        for(int index = 0; index < max_val + 1; index++)
            prime[index] = 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;
            }
        }
  
        // Store the XOR of primes in X1 and
        // the XOR of non primes in X2
        int x1 = 1, x2 = 1;
        for (int i = 0; i < n; i++) 
        {
            if (prime[arr[i]])
  
                // The number is prime
                x1 ^= arr[i];
            else if (arr[i] != 1)
  
                // The number is non-prime
                x2 ^= arr[i];
        }
  
        // Return the absolute difference
        return Math.Abs(x1 - x2);
    }
  
    // Driver Code
    public static void Main(String[] args) 
    {
        int[] arr = { 1, 3, 5, 10, 15, 7 };
        int n = arr.Length;
  
        // Find the absolute difference
        Console.WriteLine(calculateDifference(arr, n));
    }
}
  
// This code is contributed by PrinciRaj1992


输出:
4