Files

31 lines
777 B
C++

class Solution{
public:
int countPrimeSetBits(int left, int right){
int ans = 0;
for(int i = left;i<=right;i++){
if(isPrime(__builtin_popcount(i)))
ans++;
}
return ans;
}
bool isPrime(int n){
if(n<2) return false;
for(int i =2;i*i<=n;i++)
if(n%i == 0) return false;
return true;
}
};
//TC : o(right-left+1)
//SC : o(1)
/*
int countPrimeSetBits(int L, int R) {
int cnt = 0, hash[20] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1};
for (int i = L; i <= R; i++) {
bitset<20> b(i);
if(hash[b.count()]) cnt++;
}
return cnt;
}
*/