Menu Close

Find the count of numbers having a prime number in their binary representation.

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation.

l=int(input("Enter the integer L : "))
r=int(input("Enter the integer R: "))
A=[]
prime=0
for i in range(l,r+1):
	count=bin(i).count('1')
	flag=0
	for k in range(2,r+1):
		if count%k==0 and count!=k :
			flag=1
			break
	if(flag==0 and count!=1):
		prime+=1
print(prime)

Input:
       Enter the integer L : 6
       Enter the integer R: 10
Output:
       4


Input:
       Enter the integer L : 10
       Enter the integer R: 15
Output:
       5


 

Output:
Enter the integer L : 6
Enter the integer R: 10
4

Output:
Enter the integer L : 10
Enter the integer R: 15
5

Output:
Enter the integer L : 20
Enter the integer R: 30
7

Output:
Enter the integer L : 20
Enter the integer R: 20
1

Output:
Enter the integer L : 6
Enter the integer R: 8
2


More Codes to Fcuk

Explore