3 Divisors | Practice | GeeksforGeeks

You have given a list of q queries and for every query, you are given an integer N.  The task is to find how many numbers(less than or equal to N) have number of divisors exactly equal to 3. 
Example 1:
Input:
q = 1
query[0] = 6
Output:
1
Explanation:
There is only one number 4 which has
exactly three divisors 1, 2 and 4 and
less than equal to 6.
Example 2:
Input:
q = 2
query[0] = 6
query[1] = 10
Output:
1
2
Explanation:
For query 1 it is covered in the
example 1.
query 2: There are two numbers 4 and 9
having exactly 3 divisors and less than
equal to 10.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function threeDivisors() which takes an integer q and a list of integer of size q as input parameter and returns the list containing the count of the numbers having exactly 3 divisors for each query.

Expected Time Complexity: O(NlogN), 
Expected Auxiliary Space: O(N), where N is min(10^6,N)
Constraints :
1 <= q <= 103
1 <= query[i] <= 1012
+1
sourabhgharad19061 month ago
 static ArrayList<Integer> threeDivisors(ArrayList<Long> query, int q){
        // code here
        ArrayList<Integer> res = new ArrayList<>();
        long max = 0;
        for(int i=0;i<q;i++){
            max = Math.max(max,query.get(i));
        }
        long nmax = (long)Math.sqrt(max); //nmax means new maximum
        boolean arr[] = new boolean[(int)nmax+1];
        Arrays.fill(arr,true);
        for(int i=2;i<=nmax;i++){
            if(arr[i]){
                for(int j=2;j*i<=nmax;j++){
                    arr[j*i] = false;
                }
            }
        }
        int sum = 0;
        int ans[] = new int[arr.length];
        for(int i=2;i<arr.length;i++){
            if(arr[i]){
                sum++;
            }
            ans[i] = sum;
        }
        for(int i=0;i<query.size();i++){
            res.add((int)ans[(int)Math.sqrt(query.get(i))]);
        }
        return res;
    }
    
0
sourabhgharad1906
This comment was deleted.
0
santosh9yogi5 months ago
// JavaScript
// running time is too much plz suggest me to reduce running timing
class Solution{
    threeDivisors(query, q){
        //code here
        let ans = 0;
        
        for(let i = 1; i < query; i++){
            let  j = 1;
            let count = 0;
            while(j <= i ){
                
                if( i%j === 0){
                    count++;
                }else continue;
                
            }
            if(count == 3){
                ans++;
            }
            else continue;
        }
        return ans;
    }
}
0
kstgeek1236 months ago
q = int(input())for i in range(q):    n = int(input())    L=[]    k=0    i=1    while i*i <= n:        L.append(i)        i += 1    for x in L:        if x**0.5 != int(x**0.5):            #print(x)            k += 1    print(k) 
0
user0037
This comment was deleted.
+1
Raushan Kumar8 months ago
Raushan Kumar
class Solution{public:    int m = 1000001;        void seive(vector<long long="">&isprime,vector<long long="">&primes){             isprime[0] = 0;        isprime[1] = 0;                for(int i = 2; i <= sqrt(m); i++){                        if(isprime[i]){                for(int j = 2; j*i <= m; j++){                    isprime[j*i] = 0;                }            }        }        int count = 0;        for(int i = 2; i <= m; i++){                        if(isprime[i])            count++;                        primes[i] = count;        }    }        vector<int> threeDivisors(vector<long long=""> query, int q)    {                vector<long long="">isprime(1000001,1);        vector<long long="">primes(1000001,0);                seive(isprime,primes);                vector<int>v;        for(int i = 0; i < q; i++){            int p = primes[int(sqrt(query[i]))];            v.push_back(p);        }                return v;    }};
0
DiedToday9 months ago
DiedToday
Just Count  the  prime numbers whose square lies between  1 to N.
0
Mufeed Amir1 year ago
Mufeed Amir
PYTHON SOLUTION-0.21 sec
class Solution:    def threeDivisors(self, query, q):        import math as mt        n=mt.floor(mt.sqrt(max(query)))        arr=[False for x in range(0,n+1)]        arr[0]=arr[1]=True        i=2        while i<=mt.sqrt(n):            for j in range(i*i,n+1,i):                arr[j]=True
            i=i+1        a=0        aa=[]        for y in range(0,n+1):            if arr[y]==False:                a=a+1            aa.append(a)        bomb=[]        for i in range(0,q):            b=aa[mt.floor(mt.sqrt(query[i]))]            bomb.append(b)        return bomb
0
Pramod Tarpe1 year ago
Pramod Tarpe
vector<int> threeDivisors(vector<long long=""> query, int q){    int sieve[1000001];    for(int i=2;i<1000001;i++){        sieve[i] = 1;    }    sieve[0] = sieve[1] = 0;    for(long long i=0;i<1000001;i++){        if(sieve[i]&1){          long long index = i*i;          while(index < 1000001){            sieve[index] = 0;            index += i;          }        }    }    for(int i=1;i<1000001;i++){    sieve[i] += sieve[i-1];    }    vector<int> ans;    for(int i=0;i<q;i++){ long="" long="" root="sqrt(query[i]);" ans.push_back(sieve[root]);="" }="" return="" ans;="" }<="" code="">
0
Akash Kumar Seth1 year ago
Akash Kumar Seth
Python solutiondef threeDivisors(self, query, q):        # square of prime number less than or equal to query satisfy the condition         result = [0]*q        sortedQueryIndex = sorted(range(q), key=query.__getitem__)        query.sort()        # find primeSquare of max query        x = int(max(query) ** 0.5)        primeSquare = self.primeNumSquare(x)         #set the num of divisor for each query to result        count = 0        i = 0        for j in range(q):            while i < len(primeSquare) and primeSquare[i] <= query[j]:                count += 1                i += 1            result[sortedQueryIndex[j]] = count        return result    def primeNumSquare(self, n):        if n < 2:            return 0        primes = [1] * (n + 1)        primeSquare = []        for i in range(2, n + 1):            if primes[i]:                for j in range(i, n + 1, i):                    primes[j] = 0                primeSquare.append(i ** 2)        return primeSquare
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.