Given a positive integer N, the task is to find the XOR of the first N prime numbers.
Input: N = 3
First 3 prime numbers are 2, 3 and 5.
And 2 ^ 3 ^ 5 = 4
Input: N = 5
- Create Sieve of Eratosthenes to identify if a number is prime or not in O(1) time.
- Run a loop starting from 1 until and unless we find N prime numbers.
- XOR all the prime numbers and neglect those which are not prime.
- Finally, print the XOR of the 1st N prime numbers.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Find XOR of two number without using XOR operator
- Given a set, find XOR of the XOR's of all subsets.
- Find the Product of first N Prime Numbers
- Find prime numbers in the first half and second half of an array
- XOR of all Prime numbers in an Array
- XOR of K largest prime and composite numbers from the given array
- XOR of K smallest prime and composite numbers from the given array
- XOR of all Prime numbers in an Array at positions divisible by K
- Prime numbers after prime P with sum S
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.