Test

mail@pastecode.io avatar
unknown
c_cpp
a year ago
857 B
2
Indexable
Never
#include <iostream>
using namespace std;


void sieve(int isPrime[], int N) {
    isPrime[0] = 0;
    isPrime[1] = 0;
    for (int i = 2; i <= N; ++i)isPrime[i] = 1;
    for (int i = 2; i * i <= N; ++i) {
        if (isPrime[i] == 1) {
            for (int j = i * i; j <= N; j += i)
                isPrime[j] = 0;
        }
    }
}
void getResult(int isPrime[], int N)
{
    for (int i = 2; i <= N; ++i)
    {
        isPrime[i] += isPrime[i - 1];
    }
}
int main()
{
    int size = 2 * pow(10, 6) + 1;
    int* isPrime = new int[size];
    sieve(isPrime, size);
    getResult(isPrime, size);
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        int x;
        cin >> x;
        int count = 0;
        cout << isPrime[2 * x] - isPrime[x - 1] << endl;
    }
    //system("pause");
    return 0;
}