Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Sieve of Eratosthenes c++

    #include <cmath>
    #include <vector>
	using namespace std;
	int n;
    vector<int> ans;
    vector<bool> is_prime(n+1, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= sqrt(n); i++) {
        if (is_prime[i]) {
            for (int j = i * i; j <= n; j += i)
                is_prime[j] = false;
        }
    }
    for(int i=2;i<is_prime.size();i++){
        if(is_prime[i]){
            ans.push_back(i);
        }
    }
Source by cp-algorithms.com #
 
PREVIOUS NEXT
Tagged: #Sieve #Eratosthenes
ADD COMMENT
Topic
Name
1+9 =