LeetCode 762, Easy. Topics: Math, Bit Manipulation, Primality Test. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:
Problem
Given two integers left and right, return the count of numbers in the inclusive range [left, right] having a prime 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,
21 written in binary is 10101, which has 3 set bits.
Examples
Constraints
- 1 <= left <= right <= 10<sup>6</sup>
- 0 <= right - left <= 10<sup>4</sup>
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026