Skip to content

Instantly share code, notes, and snippets.

@ichaos
Created October 4, 2013 11:17
Show Gist options
  • Save ichaos/6824403 to your computer and use it in GitHub Desktop.
Save ichaos/6824403 to your computer and use it in GitHub Desktop.
find all the primes from 1 to n quickly.
/* the Sieve of Eratosthenes */
#include <cmath>
#include <vector>
using namespace std;
bool[] findPrimes(int n) {
vector<bool> isPrimes(n + 1, true);
isPrimes[0] = false;
isPrimes[1] = false;
int m = sqrt(n);
for (int i = 2; i <= m; i++) {
if (isPrimes[i]) {
for (int k = i * i; k <= m; k += i) {
isPrimes[k] = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment