Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

prime numbers less than a given number c++

// This Function returns a vector containing all primes less than n using seive of eratosthenes
vector<long long> primes_less_than(long long n)
{
    vector<long long> ans(0);
    if (n <= 2)
        return ans;
    map<long long, bool> is_prime;
    for(long long i=0; i < n; i++) 
      	is_prime[i] = true;
    is_prime[0] = false;
    is_prime[1] = false;

    for (long long i = 2; i < sqrt(n); i++)
        if (is_prime[i])
            for (long long j = i * i; j < n; j += i)
                is_prime[j] = false;

    for(long long i=0; i < n; i++)
      	if (is_prime[i] == true) 
          	ans.push_back(i);
    return ans;
}
 
PREVIOUS NEXT
Tagged: #prime #numbers #number
ADD COMMENT
Topic
Name
3+9 =