• + 0 comments

    Refrence to @ian16

    #include <bits/stdc++.h>
    #define ull unsigned long long
    
    using namespace std;
    
    string ltrim(const string &);
    string rtrim(const string &);
    
    
    /*
     * Complete the 'primeCount' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts LONG_INTEGER n as parameter.
     */
    
    ull gcd(ull a, ull b){
        while(b){
            ull t = b;
            b = a%b;
            a = t;
        }
        return a;
    }
    
    int primeCount(long n) {
        int count;
        ull prod;
        ull prime;
        
        if (n<2) return 0;
        prod = 2;
        count = 1;
        
        for(prime = 3; prod*prime<=n; prime+=2){
            if (gcd(prod, prime) == 1){
                prod *= prime;
                count++;
            }
        }
        return count;
    }
    
    int main()
    {
        ofstream fout(getenv("OUTPUT_PATH"));
    
        string q_temp;
        getline(cin, q_temp);
    
        int q = stoi(ltrim(rtrim(q_temp)));
    
        for (int q_itr = 0; q_itr < q; q_itr++) {
            string n_temp;
            getline(cin, n_temp);
    
            long n = stol(ltrim(rtrim(n_temp)));
    
            int result = primeCount(n);
    
            fout << result << "\n";
        }
    
        fout.close();
    
        return 0;
    }
    
    string ltrim(const string &str) {
        string s(str);
    
        s.erase(
            s.begin(),
            find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
        );
    
        return s;
    }
    
    string rtrim(const string &str) {
        string s(str);
    
        s.erase(
            find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
            s.end()
        );
    
        return s;
    }