Prime Set Bits in Range

EasyBit ManipulationMath

Description

Count integers i in the inclusive range [l, r] whose binary representation has a prime number of set bits.

Examples

Input:l = 6, r = 10
Output:4
Explanation:

Bits counts 2,3,1,2,2 — four are prime (2 or 3).

Input:l = 10, r = 15
Output:5
Explanation:

Counts 2,2,3,2,3,4 — five are prime.

Input:l = 1, r = 1
Output:0
Explanation:

popcount(1)=1, not prime.

Constraints

  • 1 ≤ l ≤ r ≤ 10⁶

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.