#include <iostream>
#include <vector>
#include <chrono> // Para medir el tiempo
using namespace std;
// Función para generar números primos usando la Criba de Eratóstenes
void sieve_of_eratosthenes(int n) {
vector<bool> is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false; // 0 y 1 no son primos
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
// Imprimir los números primos
for (int i = 2; i <= n; i++) {
if (is_prime[i]) {
cout << i << " ";
}
}
cout << endl;
}
int main() {
int n = 10^7; // Cambia este valor para ajustar el límite superior
// Medir el tiempo de ejecución
auto start = chrono::high_resolution_clock::now();
sieve_of_eratosthenes(n);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = end - start;
cout << "Tiempo de ejecución: " << elapsed.count() << " segundos" << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y2hyb25vPiAvLyBQYXJhIG1lZGlyIGVsIHRpZW1wbwoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIEZ1bmNpw7NuIHBhcmEgZ2VuZXJhciBuw7ptZXJvcyBwcmltb3MgdXNhbmRvIGxhIENyaWJhIGRlIEVyYXTDs3N0ZW5lcwp2b2lkIHNpZXZlX29mX2VyYXRvc3RoZW5lcyhpbnQgbikgewogICAgdmVjdG9yPGJvb2w+IGlzX3ByaW1lKG4gKyAxLCB0cnVlKTsKICAgIGlzX3ByaW1lWzBdID0gaXNfcHJpbWVbMV0gPSBmYWxzZTsgLy8gMCB5IDEgbm8gc29uIHByaW1vcwoKICAgIGZvciAoaW50IGkgPSAyOyBpICogaSA8PSBuOyBpKyspIHsKICAgICAgICBpZiAoaXNfcHJpbWVbaV0pIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IGkgKiBpOyBqIDw9IG47IGogKz0gaSkgewogICAgICAgICAgICAgICAgaXNfcHJpbWVbal0gPSBmYWxzZTsKICAgICA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