Given two integers ‘L’ and ‘R’, we need to write a program that finds the count of numbers having prime number of set bits in their binary representation in the range [L, R].
Input : 6 10 Output : 4 6 -> 110 (2 set bits, 2 is prime) 7 -> 111 (3 set bits, 3 is prime) 9 -> 1001 (2 set bits , 2 is prime) 10->1010 (2 set bits , 2 is prime) Input : 10 15 Output : 5 10 -> 1010(2 number of set bits) 11 -> 1011(3 number of set bits) 12 -> 1100(2 number of set bits) 13 -> 1101(3 number of set bits) 14 -> 1110(3 number of set bits) 15 -> 1111(4 number of set bits) Hence total count is 5
For each number in the range [L, R], we calculate the number of set bits. Using Sieve of Eratosthenes we generate a prime array up to the last number in the range (i.e. R). If the number of set bits is prime we increase the count of the numbers and print it.
This article is contributed by Saisankar Gochhayat and Harshit Sidhwa. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Prime Number of Set Bits in Binary Representation | Set 1
- Binary representation of next number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Next greater number than N with exactly one bit different in binary representation of N
- Binary representation of previous number
- Largest number with binary representation is m 1's and m-1 0's
- Check if binary representation of a number is palindrome
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Check if binary representation of a given number and its complement are anagram
- Maximum 0's between two immediate 1's in binary representation
- Count numbers have all 1s together in binary representation
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Length of the Longest Consecutive 1s in Binary Representation
- Decimal representation of given binary string is divisible by 5 or not
Improved By : Mithun Kumar