Skip to content

Instantly share code, notes, and snippets.

@memclutter
Created June 17, 2015 09:29
Show Gist options
  • Select an option

  • Save memclutter/ea121c51fc09c42c08d4 to your computer and use it in GitHub Desktop.

Select an option

Save memclutter/ea121c51fc09c42c08d4 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes example on c++
/**
* 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