Created
June 17, 2015 09:29
-
-
Save memclutter/ea121c51fc09c42c08d4 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes example on c++
This file contains hidden or 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
| /** | |
| * Sieve of Eratosthenes example. | |
| * | |
| * In mathematics, the sieve of Eratosthenes, one of a number of prime number | |
| * sieves, is a simple, ancient algorithm for finding all prime numbers up to | |
| * any given limit. | |
| * | |
| * @see https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | |
| */ | |
| #include <iostream> | |
| using namespace std; | |
| const int N = 100000; | |
| int main(int argc, char* argv[]) | |
| { | |
| int i, j; | |
| int numbers[N]; | |
| numbers[0] = false; | |
| numbers[1] = false; | |
| // initialize an array | |
| for (i = 2; i*i < N; i++) | |
| numbers[i] = true; | |
| // search for prime numbers | |
| for (i = 2; i*i < N; i++) | |
| if (numbers[i]) | |
| for (j = i*i; j < N; j += i) | |
| numbers[j] = false; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment