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;
}
}
#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;
}
}
}
#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);
}
}
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;
}
}
Code Example |
---|
Cpp :: segmented sieve of Eratosthenes in cpp |
Cpp :: primes in range cpp |
Cpp :: include cpp |
Cpp :: sleep c++ |
Cpp :: create file c++ |
Cpp :: udo apt install dotnet-sdk-5 |
Cpp :: ray sphere intersection equation |
Cpp :: c++ Program for Sum of the digits of a given number |
Cpp :: c++ tokenize string |
Cpp :: c++ cast char to string |
Cpp :: array max and minimum element c++ |
Cpp :: Xor implementation C++ |
Cpp :: delete from front in vector c++ |
Cpp :: c++ vectors |
Cpp :: pointer address to string |
Cpp :: 2d array c++ |
Cpp :: int to hexadecimal in c++ |
Cpp :: how to remove a index from a string in cpp |
Cpp :: overload of << c++ |
Cpp :: quick sort c+++ |
Cpp :: to lowercase c++ |
Cpp :: image shapes in opencv c++ |
Cpp :: c++ struct constructor |
Cpp :: c++ capture screen as pixel array |
Cpp :: how to turn int into string c++ |
Cpp :: word equation numbers |
Cpp :: cuda shared variable |
Cpp :: ++ how to write quotation mark in a string |
Cpp :: c++ get last element in vector |
Cpp :: template c++ |