Search
 
SCRIPT & CODE EXAMPLE
 

CPP

sieve of eratosthenes c++

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;
    }
}
Comment

Sieve of Eratosthenes algorithm in C++

#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;
        }
    }
}
Comment

Sieve of Eratosthenes c++

    #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);
        }
    }
Comment

sieve of eratosthenes c++

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;
    }
}
Comment

PREVIOUS NEXT
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++ 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =