Search
 
SCRIPT & CODE EXAMPLE
 

CPP

cpp Sieve algorithm

#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

PREVIOUS NEXT
Code Example
Cpp :: c++ sieve of eratosthenes 
Cpp :: find primes in a range in c++ 
Cpp :: check uppercase c++ 
Cpp :: max_element c++ 
Cpp :: How to pause a c++ program. 
Cpp :: overload stream extract cpp 
Cpp :: cpp std list example 
Cpp :: why we use iostream in C++ programming 
Cpp :: c++ program transpose of matrix 
Cpp :: c vs c++ 
Cpp :: casting c++ 
Cpp :: combine two vectors c++ 
Cpp :: size of stack in c++ 
Cpp :: c++ splitstring example 
Cpp :: run cmd command c++ 
Cpp :: C++ Vector Iterator Syntax 
Cpp :: c++ output 
Cpp :: pascal triangle using c++ 
Cpp :: 58. Length of Last Word leetcode solution in c++ 
Cpp :: c++ program to print natural numbers from 1 to 10 in reverse order using while loop 
Cpp :: bubblesort c++ 
Cpp :: c++ get maximum value unsigned int 
Cpp :: fizzbuzz c++ 
Cpp :: how to calculate bitwise xor c++ 
Cpp :: C++ code for Dijkstra’s Algorithm 
Cpp :: dynamic allocation c++ 
Cpp :: std::copy C ++ 
Cpp :: how to generate number in c++ 
Cpp :: c for loop decrement 
Cpp :: convert all strings in vector to lowercase or uppercase c++ 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =