News Leaflets
A leading news portal.

Count of integers in range [L, R] which are not divisible by first K Prime numbers

0 90

#include <bits/stdc++.h>

using namespace std;

  

void SieveOfEratosthenes(int n,

                         vector<int>& v, int k)

{

    bool prime[n + 1];

    memset(prime, true, sizeof(prime));

  

    for (int i = 2; i * i <= n; i++) {

        if (prime[i]) {

            for (int j = i * i; j <= n;

                 j += i)

                prime[j] = false;

        }

    }

  

    

    

    for (int i = 2; i < n; i++) {

        if (prime[i]) {

            v.push_back(i);

        }

        if (v.size() == k) {

            break;

        }

    }

}

  

int total_count(int L, int R, int k)

{

    vector<int> v;

  

    

    SieveOfEratosthenes(100000, v, k);

  

    

    int arr[R + 1] = { 0 };

  

    

    

    

    for (int i = 0; i < v.size(); i++) {

  

        

        

        

        int val;

  

        if (L % v[i] == 0) {

            val = L;

        }

        else {

            val = ((L / v[i]) + 1) * v[i];

        }

        for (int j = val; j <= R; j = j

                                      + v[i]) {

            arr[j] = 1;

        }

    }

  

    int count = 0;

    for (int i = L; i <= R; i++) {

        if (arr[i] == 0) {

            count++;

        }

    }

  

    return count;

}

  

int main()

{

    int L = 10, R = 25;

    int K = 3;

  

    

    cout << total_count(L, R, K);

    return 0;

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! News Leaflets is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment