代码如下:
class Solution {
public:
bool isPrime(int x) {
if (x < 2) {
return false;
}
for (int i = 2; i * i <= x; ++i) {
if (x % i == 0) {
return false;
}
}
return true;
}
int countPrimeSetBits(int left, int right) {
int res = 0;
for(int i = left; i <= right; i ++){
int sum = 0, k = i;
while(k){
if(k & 1) sum ++;
k = k >> 1;
}
if(isPrime(sum))
res ++;
}
return res;
}
};