VOOZH about

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

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


  • Courses
  • Tutorials
  • Interview Prep

Prime Number of Set Bits in Binary Representation | Set 1

Last Updated : 19 Oct, 2023

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]. 

Examples:

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. 


Output
4

Time Complexity : Let's n = (r-l) 
so overall time complexity is N*sqrt(N)
We can optimize above solution using Sieve of Eratosthenes.
Prime Number of Set Bits in Binary Representation | Set 2

Approach#2:  Using sieve of eratosthenes

The code uses three functions: sieve_of_eratosthenes(n), count_set_bits(n), and count_prime_set_bits(l, r). The first function, sieve_of_eratosthenes(n), generates a list of Boolean values indicating whether each integer up to n is prime or not. The second function, count_set_bits(n), returns the number of set bits (binary digits equal to 1) in the binary representation of n. The third function, count_prime_set_bits(l, r), counts the number of integers between l and r (inclusive) whose binary representations have a prime number of set bits. It does so by first using the sieve_of_eratosthenes function to generate a list of primes up to the maximum number of bits in any integer between l and r. It then iterates over each integer between l and r, counts the number of set bits in its binary representation using count_set_bits, and increments a counter if that count is a prime number. The final count is returned.

Algorithm

1. Generate all primes from 1 to the maximum number of set bits possible in the binary representation of r (let's call it max_bits).
2. Loop from l to r (both inclusive).
3. Convert each decimal number to its binary representation.
4. Count the number of set bits in the binary representation.
5. Check if the count is prime or not using the pre-generated primes.
6. If the count is prime, increment the counter.
7. Return the counter as the answer.


Output
4

Time Complexity: O((r-l+1) * log log r), where log log r is the time complexity of sieve_of_eratosthenes.

Auxiliary Space: O(log log r)

Comment