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++ find_if 
Cpp :: armstrong number in cpp 
Cpp :: not in c++ 
Cpp :: how to iterate throguh a string in c++ 
Cpp :: prime factorisation of a number in c++ 
Cpp :: memcpy library cpp 
Cpp :: minimum value in array using c++ 
Cpp :: aray of functions in c++ 
Cpp :: how to add an element to std::map 
Cpp :: get value of enum cpp 
Cpp :: C++ break and continue 
Cpp :: c++ vector size 
Cpp :: functors in c++ 
Cpp :: c++ call by reference 
Cpp :: c++ fstream 
Cpp :: create copy constructor c++ 
Cpp :: c++ add two matrix 
Cpp :: c++ encapsulation 
Cpp :: c++ double is nan 
Cpp :: how to empty an array c++ 
Cpp :: how to specify the number of decimal places in c++ 
Cpp :: priority queue smallest first 
Cpp :: C++ Limit of Integer 
Cpp :: c include 
Cpp :: convert integer to string in c++ 
Cpp :: c++ compile to exe 
Cpp :: all permutations with repetition C++ 
Cpp :: size() in c++ SET 
Cpp :: vectors c++ 
Cpp :: how to create a c++ templeate 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =