#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main (int argc, char *argv[]) {
int size, i, j;
size = 100000000;
vector<bool> primes(size, 1);
for (i = 2; i <= sqrt(size); i++) {
if (primes[i]) {
for (j = 2; j*i <= size; j++) {
primes[i*j] = 0; // This line causes the time difference
}
}
}
}
I'm wondering if anyone has any ideas for making that line run faster. Alternatively, I saw an article warning against the use of vector<bool> and instead using dynamic_bitset or something similar, but I couldn't find any good tutorials on that. Suggestions for using either method more efficiently would be appreciated.

New Topic/Question
Reply




MultiQuote




|