Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luojiyin1987/c46dda6dce89a72dc2ada6ee8ea4a6dc to your computer and use it in GitHub Desktop.
Save luojiyin1987/c46dda6dce89a72dc2ada6ee8ea4a6dc to your computer and use it in GitHub Desktop.
Prime Number of Set Bits in Binary Representation
func countPrimeSetBits(L int, R int) int {
primes := [...]int{2: 1, 3: 1, 5: 1, 7: 1, 11: 1, 13: 1, 17: 1, 19: 1}
res := 0
for i := L; i <= R; i++ {
bits := 0
for n := i; n > 0; n >>= 1 {
bits += n & 1
}
res += primes[bits]
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment