VOOZH about

URL: https://www.geeksforgeeks.org/dsa/prime-number-of-set-bits-in-binary-representation-set-2/

⇱ Prime Number of Set Bits in Binary Representation | Set 2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Prime Number of Set Bits in Binary Representation | Set 2

Last Updated : 24 May, 2021

Given two integers 'L' and 'R', we need to write a program that finds the count of numbers having the prime number of set bits in their binary representation in the range [L, R].


Examples:  

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.  

Output:  

3


 

Comment