#include <iostream>
#include <vector>
#include <chrono>
using namespace std;
vector<int> sieve_of_eratosthenes(int limit) {
vector<bool> primes(limit + 1, true);
primes[0] = primes[1] = false; // 0 y 1 no son primos
for (int i = 2; i * i <= limit; ++i) {
if (primes[i]) {
for (int j = i * i; j <= limit; j += i) {
primes[j] = false;
}
}
}
vector<int> result;
for (int i = 2; i <= limit; ++i) {
if (primes[i]) result.push_back(i);
}
return result;
}
int main() {
int limit = 100000; // Cambia este valor si lo deseas
// Temporizador
auto start = chrono::high_resolution_clock::now();
vector<int> primes = sieve_of_eratosthenes(limit);
auto end = chrono::high_resolution_clock::now();
// Resultados
cout << "Primos hasta " << limit << ": " << primes.size() << " encontrados." << endl;
cout << "Tiempo de ejecuciĆ³n: "
<< chrono::duration<double>(end - start).count() << " segundos." << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y2hyb25vPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZlY3RvcjxpbnQ+IHNpZXZlX29mX2VyYXRvc3RoZW5lcyhpbnQgbGltaXQpIHsKICAgIHZlY3Rvcjxib29sPiBwcmltZXMobGltaXQgKyAxLCB0cnVlKTsKICAgIHByaW1lc1swXSA9IHByaW1lc1sxXSA9IGZhbHNlOyAgLy8gMCB5IDEgbm8gc29uIHByaW1vcwogICAgZm9yIChpbnQgaSA9IDI7IGkgKiBpIDw9IGxpbWl0OyArK2kpIHsKICAgICAgICBpZiAocHJpbWVzW2ldKSB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSBpICogaTsgaiA8PSBsaW1pdDsgaiArPSBpKSB7CiAgICAgICAgICAgICAgICBwcmltZXNbal0gPSBmYWxzZTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHZlY3RvcjxpbnQ+IHJlc3VsdDsKICAgIGZvciAoaW50IGkgPSAyOyBpIDw9IGxpbWl0OyArK2kpIHsKICAgICAgICBpZiAocHJp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