Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ segmented sieve primes

/// Using Segmented Sieve to find Primes within a range (l..r)
/// Constaints: 1<=l<=r<=10^12, r-l<=10^6

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <bitset>

using namespace std;

#define ll long long
#define llu unsigned long long
#define endl "
"
#define pb push_back

#define N 1000000

typedef vector <ll> vi;

bitset < N + 1 > numbers;
vi primes;
void sieve(){
    numbers.set();
    numbers[1] = 0;
    
    for (ll i = 2; i<N; i++){
        if (numbers[i] == 1){
            primes.pb(i);
            for (ll j = i*i; j<N; j+=i){
                numbers[j] = 0;
            }
        }
    }
}

int main(){
  
    sieve();

    ll t;
    cin>>t;
    
    while (t--){
        ll l,r;
        cin>>l>>r;
        
        float tmpSqrt = sqrt(r);
        ll sqrtR = (ll)tmpSqrt;
        if (tmpSqrt != (float)sqrtR)
            sqrtR++;
        
        ll lastPrimeIndexInRange = 0;
        while (primes[lastPrimeIndexInRange] <= sqrtR)
            lastPrimeIndexInRange++;
        
        numbers.set();
        if (l == 1)
            numbers[0] = 0;
        
        for (llu i = 0; i<lastPrimeIndexInRange; i++){
            
            ll firstMulti = (l/primes[i]) * primes[i];
            if (firstMulti < l)
                firstMulti += primes[i];
            
            for (ll j = max(firstMulti, primes[i] * primes[i]); j<=r; j+= primes[i])
                numbers[j-l] = 0;
        }
        
        for (ll i = 0; i<r-l+1; i++)
            if (numbers[i] == 1)
                cout<<i + l<<endl;
        cout<<endl;
    }
    
	return 0;
}
Comment

c++ prime sieve

#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++ segmented sieve 
Cpp :: c++ find_if 
Cpp :: c++ cout colored output xcode 
Cpp :: c++ enum 
Cpp :: C++ array sort method 
Cpp :: cpp insert overload operator 
Cpp :: How to find the suarray with maximum sum using divide and conquer 
Cpp :: setprecision c++ 
Cpp :: c++ print string 
Cpp :: cpp cin 
Cpp :: vector reverse function in c++ 
Cpp :: c++ binary search 
Cpp :: calloc c++ 
Cpp :: sort index c++ 
Cpp :: declare nullptr c++ 
Cpp :: what is c++ standard library 
Cpp :: how to find the size of a character array in c++ 
Cpp :: string to integer in c++ 
Cpp :: int to float c++ 
Cpp :: untitled goose game 
Cpp :: anagram solution in c++ 
Cpp :: memory leak in cpp 
Cpp :: Find minimum maximum element CPP 
Cpp :: convert characters to lowercase c++ 
Cpp :: how to make Dijkstra in c++ 
Cpp :: c++ vector remove all duplicate elements 
Cpp :: c++ doubly linked list 
Cpp :: SUMOFPROD2 
Cpp :: c++ check if debug or release visual studio 
Cpp :: enum c++ 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =