Type Here to Get Search Results !

Prime Number of Set Bits Solution | CodingHumans |

0


Prime Number of Set Bits 


Given two integers ‘L’ and ‘R’, write a program that finds the count of numbers having prime number of set bits in their binary representation in the range [L, R].

Input:

The first line consists of an integer T i.e number of test cases. The first and only line of each test case consists of two integers L and R. 

Output:

Print the total number of prime set bit integers ranging from L to R.

Constraints: 

1<=T<=100
1<=L<=R<=100000

Example 1:
Input:

2
6 10
10 15 

Output 1:

4
5



Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

HAVE A GOOD DAY 😁



Solution
( Java )


import java.util.*;
class CodingHumans
static boolean IsPrime(int num) 
{
if (num <= 1) {  
           return false;  
       }  
       for (int i = 2; i*i<=num; ++i) {  
           if (num % i == 0) {  
               return false;  
           }  
       }  
       return true;   
}
    public static void main(String[] args) 
    { 
       Scanner sc=new Scanner(System.in);
       int t=sc.nextInt();
       while(t--!=0) {
       int l=sc.nextInt(),r=sc.nextInt();
       int count=0;
       for(int i=l;i<=r;++i) 
       {  
       int bit=Integer.bitCount(i);
       if(IsPrime(bit)) 
       {
       ++count;
       }
       }
       System.out.println(count);
       }
    }
}

If you have any doubts regarding this problem or  need the solution in other programming languages then join the Discussion  below .

Post a Comment

0 Comments

Top Post Ad

Below Post Ad