-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path762_PrimeNumberofBitsinBinaryRepresentation.py
More file actions
38 lines (36 loc) · 1.15 KB
/
762_PrimeNumberofBitsinBinaryRepresentation.py
File metadata and controls
38 lines (36 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution:
def is_prime_number(self, x):
if x >= 2:
for y in range(2,x):
if not ( x % y ):
return False
else:
return False
return True
def countPrimeSetBits(self, L: int, R: int) -> int:
count = 0
primes = {2, 3, 5, 7, 11, 13, 17, 19}
for i in range(L, R+1):
num = bin(i).split("b")[1].count('1')
if num in primes:
count += 1
return count
def main():
print(Solution().countPrimeSetBits(6, 10))
# Output: 4
# Explanation:
# 6 -> 110 (2 set bits, 2 is prime)
# 7 -> 111 (3 set bits, 3 is prime)
# 9 -> 1001 (2 set bits , 2 is prime)
# 10->1010 (2 set bits , 2 is prime)
print(Solution().countPrimeSetBits(10, 15))
# Output: 5
# Explanation:
# 10 -> 1010 (2 set bits, 2 is prime)
# 11 -> 1011 (3 set bits, 3 is prime)
# 12 -> 1100 (2 set bits, 2 is prime)
# 13 -> 1101 (3 set bits, 3 is prime)
# 14 -> 1110 (3 set bits, 3 is prime
# 15 -> 1111 (4 set bits, 4 is not prime)
if __name__ == '__main__':
main()