XOR of all Prime numbers in an Array at positions divisible by K | Javascript 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
const MAX = 1000005 ; function SieveOfEratosthenes ( prime ) { prime [ 1 ] = false ; prime [ 0 ] = false ; for ( let p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } function prime_xor ( arr , n , k ) { let prime = new Array ( MAX ) . fill ( true ) ; SieveOfEratosthenes ( prime ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { if ( ( i + 1 ) % k == 0 ) { ans ^= arr [ i ] ; } } } document . write ( ans ) ; } let arr = [ 2 , 3 , 5 , 7 , 11 , 8 ] ; let n = arr . length ; let K = 2 ; prime_xor ( arr , n , K ) ;