Skip to content

Instantly share code, notes, and snippets.

@thiago-rezende
Last active March 12, 2019 05:18
Show Gist options
  • Save thiago-rezende/0c5a3f95ca7a308bf65b792b85797c52 to your computer and use it in GitHub Desktop.
Save thiago-rezende/0c5a3f95ca7a308bf65b792b85797c52 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <cmath>
int main()
{
int n = 0;
std::cout << "Determine um valor limite para saber os numeros primos entre 2 e este numero: ";
std::cin >> n;
int max_check = std::sqrt(n);
std::vector<bool> primes(n+1, true);
for(int i = 2; i < n; i++)
{
if(i > max_check)
break;
if(primes.at(i))
{
for(int j = 2; i * j <= n; j++)
primes.at(i*j) = false;
}
}
for(int i = 2; i < n; i++)
if(primes.at(i))
std::cout << i << " ";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment