Ctrl K

A Prime Number of Ones

Easy
You are given two integers low and high. How many numbers from low to high inclusive have a prime number of ones in their binary form? For example, 13 = 1101₂ has three ones, and 3 is prime, so 13 counts.
Example 1
low=3, high=8 → 4

3 = 11₂ (2 ones), 4 = 100₂ (1), 5 = 101₂ (2), 6 = 110₂ (2), 7 = 111₂ (3), 8 = 1000₂ (1). The count is prime for 3, 5, 6 and 7, so the answer is 4.

Example 2
low=16, high=16 → 0

16 = 10000₂ has a single one, and 1 is not prime.

Example 3
low=1, high=31 → 21
Constraints
  • 1 ≤ low ≤ high ≤ 10⁶
  • 0 ≤ high - low ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Binary Strings EasyOnly Twos Multiplied EasyThe Lost Ticket EasyOnly Fours Multiplied EasyHexadecimal Notation Easy
loading Python…
Input
low=3, high=8
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 76 tests on the server, hidden ones included.