【leetcode】二进制表示中质数个计算置位

 

bool isprime(int count)
{
    if (count ==1) return false;
    for (int j=2; j<=sqrt(count); j++) if(count % j == 0) return false; 
    return true;
}
int countPrimeSetBits(int L, int R){
    int i,count=0,sum=0,temp;
    for (i=L; i<=R; i++)
    {
        temp = i;
        while(temp)
        {
            if (temp & 1) count++;
            temp >>= 1;
        }
        if (isprime(count)) sum++;
        count=0;
    }
    return sum;
}

 

posted @ 2020-09-11 11:27  温暖了寂寞  阅读(154)  评论(0编辑  收藏  举报