Prime number of set bits | Practice | GeeksforGeeks

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].

Example 1:
Input: L = 6, R = 10
Output: 4
Explanation: 6, 7, 9, 10 having
prime set bits between 6 and 10. 


Example 2:
Input: L = 10, R = 15
Output: 5
Explanation: 10, 11, 12, 13, 14 having
prime set bits between 10 and 15.

Your Task:  
You dont need to read input or print anything. Complete the function primeSetBits() which takes L and R as input parameter and returns the count of numbers having prime number of set bits in their binary representation.

Expected Time Complexity: O(nlog(n)sqrt(n))
Expected Auxiliary Space: O(1)

Constraints:
1 <= L <= R <=1000
0
60mqcs20shivamyadavin 1 hour
#Pyhton solution
def primeSetBits(self, L, R): # code here l = 1001 siv = [0] * l siv[0] = 1 siv[1] = 1 for i in range(2,int(math.sqrt(l))+1):     if siv[i]==0:         for j in range(2*i,l,i):             siv[j] = 1 count =0 for i in range(L,R+1):     m = i     c =0     while m !=0:         m = m & m-1         c +=1     if siv[c]==0:         count +=1 return count
0
vermaashitiit1 month ago
Execution Time:0.00
int i,j,n=R+1;       bool isprime[n];       fill_n(isprime,n,true);       isprime[0]=isprime[1]=false;       for(i=2;i<n;i++){           if(isprime[i]==true){               for(j=2*i;j<n;j+=i){                   isprime[j]=false;               }           }       }       int ans=0;       for(i=L;i<=R;i++){           int c=0;             j=i;           while(j){               c++;               j=j&(j-1);           }           if(isprime[c]==true) ans++;       }       return ans;
0
aman44113 months ago
public int primeSetBits(int L, int R)   {       int ans = 0;       for(int i=L;i<=R;i++){           String binary = Integer.toBinaryString(i);           int setBits = 0;           for(char c : binary.toCharArray()){               if(c == '1'){                   setBits++;               }           }           if(isPrime(setBits)){               ans++;           }       }       return ans;   }      public static boolean isPrime(int num){       if(num == 0 || num == 1){           return false;       }              for(int i=2;i<=Math.sqrt(num);i++){           if(num%i == 0){               return false;           }       }       return true;   }
0
Aakarshak arora10 months ago
Aakarshak arora
Python3
    def primeSetBits(self, L, R):        count, co = 0, 0
        def prime(c):            if c == 2:                return True            if c % 2 == 0 or c == 1:                return False            for i in range(3, int(c / 3) + 1, 2):                if c % i == 0:                    return False            return True
        for i in range(L, R + 1):            x = bin(i)[2:]
            for j in x:                if j == '1':                    count += 1
            if prime(count):                co += 1            count = 0        return co
0
ahmar11 months ago
ahmar
Status :Passedtechnique Used : prime no logic.Lang : Javapre requisites: School level of stack.Code : https://ide.geeksforgeeks.o...
0
Bhanu1 year ago
Bhanu
The time complexity is  O(nlog(k)+nsqrt(log(k)))space complexity is O(1)
class Solution:def primeSetBits(self, L, R):    tot = 0    for i in range(L, R+1):        tSetbits = self.noOfSetBits(i)        if self.isPrime(tSetbits):            tot += 1    return tot
def noOfSetBits(self, num):    tot = 0    if num%2:       tot += 1    while num:        num >>= 1            if num%2:                tot += 1        return tot
    def isPrime(self, num):        if num == 1:            return 0        if num == 2:            return 1        for i in range(2, int(num**0.5)+2):            if num%i == 0:                return 0        return 1
0
Annanya Mathur1 year ago
Annanya Mathur
bool isprime(int n)  { if (n<2)  return false;      for(int i=2;i*i<=n;i++)      {          if(n%i==0)          return false;     }      return true;  }    int primeSetBits(int L, int R){        int c=0;        for(int i=L;i<=R;i++)        {           int p=__builtin_popcount(i);
           if(isprime(p))           c++;  }        return c;    }
0
Kausar Ahamed1 year ago
Kausar Ahamed
ET-0.48
0
mihir vaghela2 years ago
mihir vaghela
JAVA SOLUTION
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) {//codeScanner obj = new Scanner(System.in);int T = obj.nextInt();int cnt=0;while(T-->0){    int L = obj.nextInt();    int R = obj.nextInt();        for(int i = L ; i<= R ; i++)        {                  if(isPrime(Integer.bitCount(i)))                      cnt++;        }        System.out.println(cnt);        cnt=0;}
}public static boolean isPrime(int num){    if(num == 0 || num == 1)    return(false);         if(num != 2 && num %2 ==0)          return(false);    for(int i = 2 ; i*i <= num ; i++)    {        if(num % i == 0)        return(false);    }    return(true);}}
0
Lakshya Khandelwal2 years ago
Lakshya Khandelwal
using buitin function of gcc compiler:#include<bits stdc++.h="">using namespace std;bool check(int k){ if(k==1)return false;if(k==2)return true;    for(int i=2;i<=sqrt(k);i++)    {if(k%i==0)    return false;}    return true;}int main() {int t;cin>>t; while(t--) { int l,r;cin>>l>>r;int p=0;for(int i=l;i<=r;i++) {int k=__builtin_popcount(i);if(check(k))p++;}cout<
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.