Created
October 4, 2013 11:17
-
-
Save ichaos/6824403 to your computer and use it in GitHub Desktop.
find all the primes from 1 to n quickly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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