Skip to content

Instantly share code, notes, and snippets.

@eroltutumlu
Created February 10, 2015 20:48
Show Gist options
  • Save eroltutumlu/d1c1eb66183b996bfaad to your computer and use it in GitHub Desktop.
Save eroltutumlu/d1c1eb66183b996bfaad to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes Algorithm
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000000
int main(void) {
long long int i,j;
int *primes;
primes = malloc(sizeof(int)*SIZE);
for(i = 2; i < SIZE; i++)
{
primes[i] = 1;
}
for(i = 2; i < SIZE; i++)
{
for(j = i; i*j < SIZE; j++)
{
if(primes[i])
{
primes[i*j] = 0;
}
}
}
for(i = 2; i < SIZE; i++)
{
if(primes[i])
{
printf("%llu ",i);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment