patterncppModerate
Optimization on Sieve of Eratosthenes using vector<bool>
Viewed 0 times
boolsieveoptimizationusingvectoreratosthenes
Problem
I am solving this prime generator problem from SPOJ:
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
Input
The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output
For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
I’m using the Sieve of Eratosthenes. Whenever I submit the following code it gives Time Limit Exceeded. Can anyone please give me some optimizations that can make my code run faster?
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
Input
The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output
For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
I’m using the Sieve of Eratosthenes. Whenever I submit the following code it gives Time Limit Exceeded. Can anyone please give me some optimizations that can make my code run faster?
#include
#include
#include
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
int t = 0;
cin >> t;
while (t--) {
ll first = 0, n = 0;
cin >> first >> n;
vector primes(n+1, 0);
for (ll i = 2; i = first) {
cout << i << '\n';
}
}
}
return 0;
}Solution
Repeating work already done
The biggest problem with your solution is that for each test case, you start over from scratch. This causes you to do up to 10x the work that you need to do. What you should do instead is:
Don't use expensive operations in loop iterator
Depending on your compiler, this line could be costly:
You could instead do:
to make sure that you don't call
Sieve loop improvements
This sieve loop:
Could be written more optimally as:
Notice, the loop starts at
Printing loop
The printing loop is not optimal because it starts at 2, even though
You should just start the loop at
The biggest problem with your solution is that for each test case, you start over from scratch. This causes you to do up to 10x the work that you need to do. What you should do instead is:
- Read all test cases first (into a vector for example).
- Find the maximum
nof all the test cases.
- Use your sieve once to find all primes up to the maximum number.
- Using the vector of primes that you produced with the sieve, solve all test cases.
Don't use expensive operations in loop iterator
Depending on your compiler, this line could be costly:
for (ll i = 2; i <= sqrt(n); ++i) {You could instead do:
for (ll i = 2, sqrt_n = sqrt(n); i <= sqrt_n; ++i) {to make sure that you don't call
sqrt() once per loop iteration.Sieve loop improvements
This sieve loop:
for (ll j = 2; (i*j) <= n; ++j) {
primes[i*j] = 1;
}Could be written more optimally as:
for (ll j = i*i, dj = i+i; j <= n; j += dj) {
primes[j] = 1;
}Notice, the loop starts at
ii instead of 2i, and it iterates by 2*i instead of by i. It also avoids multiplication within the main loop. However, this loop assumes that all even numbers have already been marked from the primes array, so you will have to do that separately when you create the primes array.Printing loop
The printing loop is not optimal because it starts at 2, even though
first may be very large:for (ll i = 2; i = first) {
cout << i << '\n';
}
}You should just start the loop at
first:for (ll i = first; i <= n; ++i) {
if (primes[i] == 0) {
cout << i << '\n';
}
}Code Snippets
for (ll i = 2; i <= sqrt(n); ++i) {for (ll i = 2, sqrt_n = sqrt(n); i <= sqrt_n; ++i) {for (ll j = 2; (i*j) <= n; ++j) {
primes[i*j] = 1;
}for (ll j = i*i, dj = i+i; j <= n; j += dj) {
primes[j] = 1;
}for (ll i = 2; i <= n; ++i) {
if (primes[i] == 0 && i >= first) {
cout << i << '\n';
}
}Context
StackExchange Code Review Q#112227, answer score: 10
Revisions (0)
No revisions yet.