XOR of all Prime numbers in an Array at positions divisible by K | C ++ program to find XOR of all Prime numbers in an Array at positions divisible by K ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to find the required XOR ; To store XOR of the primes ; Traverse the array ; If the number is a prime ; If index is divisible by k ; Print the xor ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000005 NEW_LINE void SieveOfEratosthenes ( vector < bool > & prime ) { prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } void prime_xor ( int arr [ ] , int n , int k ) { vector < bool > prime ( MAX , true ) ; SieveOfEratosthenes ( prime ) ; long long int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { if ( ( i + 1 ) % k == 0 ) { ans ^= arr [ i ] ; } } } cout << ans << endl ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 7 , 11 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; prime_xor ( arr , n , K ) ; return 0 ; }