Last active
September 29, 2015 11:38
-
-
Save kuoe0/1594929 to your computer and use it in GitHub Desktop.
[Prime] Sieve of Eratosthenes - http://kuoe0.ch/1209/prime-sieve-of-eratosthenes/
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
#include <cstdio> | |
#include <cstdlib> | |
using namespace std; | |
#define MAX 1000000 | |
bool isNotPrime[ MAX + 1 ]; | |
void sieve() { | |
memset( isNotPrime, 0, sizeof( isNotPrime ) ); | |
isNotPrime[ 0 ] = isNotPrime[ 1 ] = 1; | |
for ( int i = 2; i <= MAX; i += 2 ) | |
isNotPrime[ i ] = 1; | |
for ( int i = 3; i * i <= sqrt( MAX ); i += 2 ) | |
if ( !isNotPrime[ i ] ) | |
for ( int j = i + i; j <= MAX; j += i + i ) | |
isNotPrime[ j ] = 1; | |
} | |
int main() { | |
sieve(); | |
for ( int i = 0; i <= MAX; ++i ) | |
if ( !isNotPrime[ i ] ) | |
printf( "%d\n", i ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment