class Solution {
public:
    bool IsPrime(int n) {
        if (n == 1)
        {
            return false;
        }
        if (n == 2 || n == 3)
        {
            return true;
        }
        for (int i = 2; i <= sqrt(n); i++) {
            if (n%i == 0) {
                return false;
            }
        }
        return true;
    }
    int CountOnes(int n)
    {
        int sum = 0;
        while (n)
        {
            int x = n & 1;
            if (x)
            {
                sum++;
            }
            n >>= 1;
        }
        return sum;
    }
    int countPrimeSetBits(int L, int R) {
        int sum = 0;
        for (int i = L; i <= R; i++)
        {
            int x = CountOnes(i);
            if (IsPrime(x))
            {
                sum++;
            }
        }
        return sum;
    }
};

 

posted on 2018-09-29 11:45  Sempron2800+  阅读(102)  评论(0编辑  收藏  举报