Leetcode 767 Solution

This article provides solution to leetcode question 767 (prime-number-of-set-bits-in-binary-representation)

https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation

Solution

class Solution:
    def countPrimeSetBits(self, L: int, R: int) -> int:
        primes = {2, 3, 5, 7, 11, 13, 17, 19}

        ans = 0
        for x in range(L, R + 1):
            cnt = 0
            while x:
                x &= x - 1
                cnt += 1
            if cnt in primes:
                ans += 1
        return ans