prime numbers less than a given number c 2b 2b

Solutions on MaxInterview for prime numbers less than a given number c 2b 2b by the best coders in the world

showing results for - "prime numbers less than a given number c 2b 2b"
Agustín
04 May 2019
1// This Function returns a vector containing all primes less than n using seive of eratosthenes
2vector<long long> primes_less_than(long long n)
3{
4    vector<long long> ans(0);
5    if (n <= 2)
6        return ans;
7    map<long long, bool> is_prime;
8    for(long long i=0; i < n; i++) 
9      	is_prime[i] = true;
10    is_prime[0] = false;
11    is_prime[1] = false;
12
13    for (long long i = 2; i < sqrt(n); i++)
14        if (is_prime[i])
15            for (long long j = i * i; j < n; j += i)
16                is_prime[j] = false;
17
18    for(long long i=0; i < n; i++)
19      	if (is_prime[i] == true) 
20          	ans.push_back(i);
21    return ans;
22}