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

c++ sieve of eratosthenes

#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 :: powers of 2 in cpp 
Cpp :: c++ ascii value 
Cpp :: passing a 2d array cpp 
Cpp :: Casino Number Guessing Game - C++ 
Cpp :: how to pass arrays by reference c++ 
Cpp :: random c++ 
Cpp :: type casting in cpp 
Cpp :: binpow in fenwick tree 
Cpp :: c++ split string by sstream 
Cpp :: declare a variable in cpp 
Cpp :: c++ define constant 
Cpp :: how to make sound in c++ 
Cpp :: run with cpp version 
Cpp :: c++ forloop 
Cpp :: qregexpvalidator qlineedit email address 
C :: find string in all files powershell 
C :: full installation of clang in ubuntu 
C :: how to make a hello world program in c 
C :: check if string starts with c 
C :: dvlprroshan 
C :: size of an array c 
C :: add 2 numbers in c 
C :: c program 
C :: how to sleep in c 
C :: addition in c 
C :: sum average min max in c array 
C :: convert int to string c 
C :: c get current month, year, day 
C :: warning: function returns address of local variable [-Wreturn-local-addr] 
C :: bootstrap form 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =