xxxxxxxxxx
int n;
vector<bool> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i] && (long long)i * i <= n) {
for (int j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
xxxxxxxxxx
#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);
}
}
xxxxxxxxxx
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#define N 1000000 //N is the Range (0..N)
bitset < N+1 > numbers;
vector < int > primes;
void sieve(){
numbers.set();
numbers[1] = 0;
for (int i = 2; i < N; i++){
if (numbers[i] == 1){
cout<<i<<endl;
primes.push_back(i);
for (int j = i*i; j<=N; j+=i)
numbers[j] = 0;
}
}
}
xxxxxxxxxx
#include <vector>
std::vector<bool> sieveOfEratosthenes(int limit) {
std::vector<bool> isPrime(limit + 1, true);
isPrime[0] = isPrime[1] = false;
for (int p = 2; p * p <= limit; ++p) {
if (isPrime[p]) {
for (int i = p * p; i <= limit; i += p)
isPrime[i] = false;
}
}
return isPrime;
}
bool isPrime(int number) {
if (number <= 1)
return false;
std::vector<bool> isPrimeList = sieveOfEratosthenes(number);
return isPrimeList[number];
}
xxxxxxxxxx
int n;
vector<char> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i] && (long long)i * i <= n) {
for (int j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}