Print all possible pair with prime XOR in the Array | C ++ implementation of the above approach ; Function for Sieve of Eratosthenes ; If i is prime , then make all multiples of i false ; Function to print all Pairs whose XOR is prime ; if A [ i ] ^ A [ j ] is prime , then print this pair ; Driver Code ; Generate all the prime number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sz = 1e5 ; bool isPrime [ sz + 1 ] ; void generatePrime ( ) { int i , j ; memset ( isPrime , true , sizeof ( isPrime ) ) ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } void Pair_of_PrimeXor ( int A [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( isPrime [ ( A [ i ] ^ A [ j ] ) ] ) { cout << " ( " << A [ i ] << " , ▁ " << A [ j ] << " ) ▁ " ; } } } } int main ( ) { int A [ ] = { 1 , 3 , 6 , 11 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; generatePrime ( ) ; Pair_of_PrimeXor ( A , n ) ; return 0 ; }