Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Prime Number of Set Bits in Binary Representation in Python
Suppose we have two integers L and R, we have to find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary form.
So, if the input is like L = 6 and R = 10, then the output will be 4, as there are 4 numbers 6(110), 7(111), 9(1001), 10(1010), all have prime number of set bits.
Understanding Set Bits
Set bits are the bits that are '1' in the binary representation of a number. For example:
- 6 in binary is 110, which has 2 set bits
- 7 in binary is 111, which has 3 set bits
- 9 in binary is 1001, which has 2 set bits
- 10 in binary is 1010, which has 2 set bits
Algorithm
To solve this, we will follow these steps −
- count := 0
- for j in range L to R, do
- if set bit count of j is in [2,3,5,7,11,13,17,19], then
- count := count + 1
- return count
Implementation
Let us see the following implementation to get better understanding −
class Solution:
def countPrimeSetBits(self, L, R):
def popcount(i):
return bin(i)[2:].count('1')
count = 0
for j in range(L, R + 1):
if popcount(j) in [2, 3, 5, 7, 11, 13, 17, 19]:
count += 1
return count
# Test the solution
ob = Solution()
result = ob.countPrimeSetBits(6, 10)
print(f"Count of numbers with prime set bits between 6 and 10: {result}")
Count of numbers with prime set bits between 6 and 10: 4
Step-by-Step Breakdown
Let's trace through each number in the range [6, 10] −
def analyze_range(L, R):
primes = [2, 3, 5, 7, 11, 13, 17, 19]
for num in range(L, R + 1):
binary = bin(num)[2:]
set_bits = binary.count('1')
is_prime_bits = set_bits in primes
print(f"{num}: {binary} ? {set_bits} set bits ? {'Prime' if is_prime_bits else 'Not Prime'}")
analyze_range(6, 10)
6: 110 ? 2 set bits ? Prime 7: 111 ? 3 set bits ? Prime 8: 1000 ? 1 set bits ? Not Prime 9: 1001 ? 2 set bits ? Prime 10: 1010 ? 2 set bits ? Prime
Alternative Implementation
Here's a more efficient approach using bit manipulation −
def countPrimeSetBits(L, R):
def count_set_bits(n):
count = 0
while n:
count += 1
n &= n - 1 # Remove the rightmost set bit
return count
prime_set = {2, 3, 5, 7, 11, 13, 17, 19}
count = 0
for num in range(L, R + 1):
if count_set_bits(num) in prime_set:
count += 1
return count
# Test the alternative implementation
result = countPrimeSetBits(6, 10)
print(f"Result using bit manipulation: {result}")
Result using bit manipulation: 4
Key Points
- We only need to check for prime numbers up to 19 because the maximum number of bits in a 32-bit integer is 32, but we typically work with smaller ranges
- The
bin()function returns a string starting with '0b', so we slice it with[2:] - The bit manipulation approach using
n &= n - 1is more efficient for counting set bits
Conclusion
This problem combines binary representation, bit counting, and prime number checking. The solution iterates through the range and counts numbers whose set bits form a prime number, making it an interesting application of bit manipulation in Python.
