classSolution { boolisPrime(int x){ if (x < 2) { returnfalse; } for (int i = 2; i * i <= x; ++i) { if (x % i == 0) { returnfalse; } } returntrue; }
public: intcountPrimeSetBits(int left, int right){ int ans = 0; for (int x = left; x <= right; ++x) { if (isPrime(__builtin_popcount(x))) { ++ans; } } return ans; } };
publicclassSolution { publicintCountPrimeSetBits(int left, int right) { int ans = 0; for (int x = left; x <= right; ++x) { if (IsPrime(BitCount(x))) { ++ans; } } return ans; }
privateboolIsPrime(int x) { if (x < 2) { returnfalse; } for (int i = 2; i * i <= x; ++i) { if (x % i == 0) { returnfalse; } } returntrue; }
privatestaticintBitCount(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0f0f0f0f; i = i + (i >> 8); i = i + (i >> 16); return i & 0x3f; } }
boolisPrime(int x) { if (x < 2) { returnfalse; } for (int i = 2; i * i <= x; ++i) { if (x % i == 0) { returnfalse; } } returntrue; }
intcountPrimeSetBits(int left, int right){ int ans = 0; for (int x = left; x <= right; ++x) { if (isPrime(__builtin_popcount(x))) { ++ans; } } return ans; }
var countPrimeSetBits = function(left, right) { let ans = 0; for (let x = left; x <= right; ++x) { if (isPrime(bitCount(x))) { ++ans; } } return ans; };
constisPrime = (x) => { if (x < 2) { returnfalse; } for (let i = 2; i * i <= x; ++i) { if (x % i === 0) { returnfalse; } } returntrue; }
我们可以用一个二进制数 mask}=665772=10100010100010101100_{2 来存储这些质数,其中 mask 二进制的从低到高的第 i 位为 1 表示 i 是质数,为 0 表示 i 不是质数。
设整数 x 的二进制中 1 的个数为 c,若 mask 按位与 2^c 不为 0,则说明 c 是一个质数。
[sol2-Python3]
1 2 3
classSolution: defcountPrimeSetBits(self, left: int, right: int) -> int: returnsum(((1 << x.bit_count()) & 665772) != 0for x inrange(left, right + 1))
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: intcountPrimeSetBits(int left, int right){ int ans = 0; for (int x = left; x <= right; ++x) { if ((1 << __builtin_popcount(x)) & 665772) { ++ans; } } return ans; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11
classSolution { publicintcountPrimeSetBits(int left, int right) { intans=0; for (intx= left; x <= right; ++x) { if (((1 << Integer.bitCount(x)) & 665772) != 0) { ++ans; } } return ans; } }
publicclassSolution { publicintCountPrimeSetBits(int left, int right) { int ans = 0; for (int x = left; x <= right; ++x) { if (((1 << BitCount(x)) & 665772) != 0) { ++ans; } } return ans; }
privatestaticintBitCount(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0f0f0f0f; i = i + (i >> 8); i = i + (i >> 16); return i & 0x3f; } }
[sol2-Golang]
1 2 3 4 5 6 7 8
funccountPrimeSetBits(left, right int) (ans int) { for x := left; x <= right; x++ { if1<<bits.OnesCount(uint(x))&665772 != 0 { ans++ } } return }
[sol2-C]
1 2 3 4 5 6 7 8 9
intcountPrimeSetBits(int left, int right){ int ans = 0; for (int x = left; x <= right; ++x) { if ((1 << __builtin_popcount(x)) & 665772) { ++ans; } } return ans; }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13
var countPrimeSetBits = function(left, right) { let ans = 0; for (let x = left; x <= right; ++x) { if (((1 << bitCount(x)) & 665772) != 0) { ++ans; } } return ans; };