Skip to content

Instantly share code, notes, and snippets.

@kuoe0
Last active September 29, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuoe0/1594929 to your computer and use it in GitHub Desktop.
Save kuoe0/1594929 to your computer and use it in GitHub Desktop.
[Prime] Sieve of Eratosthenes - http://kuoe0.ch/1209/prime-sieve-of-eratosthenes/
#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