Created
July 24, 2018 02:37
-
-
Save luojiyin1987/c46dda6dce89a72dc2ada6ee8ea4a6dc to your computer and use it in GitHub Desktop.
Prime Number of Set Bits in Binary Representation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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