Prime Number of Set Bits in Binary Representation
EasyUpdated: Oct 13, 2025
Practice on:
Problem
Given two integers left and right, return _thecount of numbers in the
inclusive range _[left, right]having aprime number of set bits in their binary representation.
Recall that the number of set bits an integer has is the number of 1's present when written in binary.
- For example,
21written in binary is10101, which has3set bits.
Examples
Example 1
Input: left = 6, right = 10
Output: 4
Explanation:
6 -> 110 (2 set bits, 2 is prime)
7 -> 111 (3 set bits, 3 is prime)
8 -> 1000 (1 set bit, 1 is not prime)
9 -> 1001 (2 set bits, 2 is prime)
10 -> 1010 (2 set bits, 2 is prime)
4 numbers have a prime number of set bits.
Example 2
Input: left = 10, right = 15
Output: 5
Explanation:
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)
15 -> 1111 (4 set bits, 4 is not prime)
5 numbers have a prime number of set bits.
Constraints
1 <= left <= right <= 10^60 <= right - left <= 10^4
Solution
Method 1 - Precompute Primes & Bit Count
Intuition
For each number in the range [left, right], count the number of set bits (1s) in its binary representation (call it cnt). If cnt is a prime number, increment the answer. Since the maximum number of set bits for numbers up to 10^6 is at most 20, we can precompute all primes up to 20 in a primes set and test membership in O(1).
Approach
- Precompute the set
primesof prime integers up to 20. - For each integer
xin the inclusive rangeleft..right, computecnt= number of set bits inx(use a built-in or bitwise method). - If
cntis inprimes, increment the running answer. - Return the final answer.
Code
C++
#include <vector>
#include <unordered_set>
using namespace std;
int countPrimeSetBits(int left, int right) {
unordered_set<int> primes = {2,3,5,7,11,13,17,19};
int ans = 0;
for (int x = left; x <= right; ++x) {
int cnt = __builtin_popcount(x);
if (primes.count(cnt)) ++ans;
}
return ans;
}
Go
func countPrimeSetBits(left int, right int) int {
primes := map[int]bool{2:true,3:true,5:true,7:true,11:true,13:true,17:true,19:true}
ans := 0
for x := left; x <= right; x++ {
cnt := 0
y := x
for y > 0 {
cnt += y & 1
y >>= 1
}
if primes[cnt] {
ans++
}
}
return ans
}
Java
import java.util.*;
class Solution {
public int countPrimeSetBits(int left, int right) {
Set<Integer> primes = Set.of(2,3,5,7,11,13,17,19);
int ans = 0;
for (int x = left; x <= right; ++x) {
int cnt = Integer.bitCount(x);
if (primes.contains(cnt)) ++ans;
}
return ans;
}
}
Kotlin
fun countPrimeSetBits(left: Int, right: Int): Int {
val primes = setOf(2,3,5,7,11,13,17,19)
var ans = 0
for (x in left..right) {
val cnt = Integer.bitCount(x)
if (cnt in primes) ans++
}
return ans
}
Python
def countPrimeSetBits(left: int, right: int) -> int:
primes = {2,3,5,7,11,13,17,19}
ans = 0
for x in range(left, right+1):
cnt = bin(x).count('1')
if cnt in primes:
ans += 1
return ans
Rust
fn count_prime_set_bits(left: i32, right: i32) -> i32 {
let primes = [2,3,5,7,11,13,17,19];
let mut ans = 0;
for x in left..=right {
let cnt = x.count_ones();
if primes.contains(&(cnt as i32)) {
ans += 1;
}
}
ans
}
TypeScript
export function countPrimeSetBits(left: number, right: number): number {
const primes = new Set([2,3,5,7,11,13,17,19]);
let ans = 0;
for (let x = left; x <= right; ++x) {
let cnt = 0, y = x;
while (y > 0) {
cnt += y & 1;
y >>= 1;
}
if (primes.has(cnt)) ans++;
}
return ans;
}
Complexity
- ⏰ Time complexity:
O(N log M), where N = right - left + 1, M is the maximum value (up to 10^6), log M for counting set bits. - 🧺 Space complexity:
O(1)(ignoring output and the set of small primes).