Given two integers ‘L’ and ‘R’, write a program to find the total numbers that are having prime number of set bits in their binary representation in the range [L, R].
Input : l = 6, r = 10 Output : 4 Explanation : 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) Hence count is 4 Input : l = 10, r = 15 Output : 5 10 -> 1010 (2 set bits, 2 is prime) 11 -> 1011 (3 set bits, 3 is prime) 12 -> 1100 (2 set bits, 2 is prime) 13 -> 1101 (3 set bits, 3 is prime) 14 -> 1110 (3 set bits, 3 is prime) Hence count is 5
Explanation: In this program we find a total number, that’s having prime number of set bit. so we use a CPP predefined function __builtin_popcount() these functions provide a total set bit in number. as well as be check the total bit’s is prime or not if prime we increase the counter these process repeat till given range.
Time Complexity : Let’s n = (r-l)
so overall time complexity is N*sqrt(N)
We can optimize above solution using Sieve of Eratosthenes.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Prime Number of Set Bits in Binary Representation | Set 2
- Number of mismatching bits in the binary representation of two integers
- Check if all the set bits of the binary representation of N are at least K places away
- Count of total bits toggled/flipped in binary representation of 0 to N
- Binary representation of a given number
- 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
- Largest number with binary representation is m 1's and m-1 0's
- Binary representation of previous number
- Next greater number than N with exactly one bit different in binary representation of N
- Number of leading zeros in binary representation of a given number
- Check if binary representation of a number is palindrome
- Occurrences of a pattern in binary representation of a number
- Find the n-th number whose binary representation is a palindrome
- Check if binary representation of a given number and its complement are anagram
- Check if actual binary representation of a number is palindrome
- Smallest multiple of N with exactly N digits in its Binary number representation
- Find consecutive 1s of length >= n in binary representation of a number
- C program to count zeros and ones in binary representation of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : Mithun Kumar