Answers for "prime numbers less than a given number c++"

C++
0

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;
}
Posted by: Guest on June-19-2021

Code answers related to "prime numbers less than a given number c++"

Browse Popular Code Answers by Language