Skip to content

Instantly share code, notes, and snippets.

@darkf
Created July 28, 2012 07:39
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 darkf/3192274 to your computer and use it in GitHub Desktop.
Save darkf/3192274 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in C99
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#define n 100
static bool A[n];
int main() {
int i, j;
int upper_bound = (int) ceil(sqrt(n));
memset(A, true, sizeof(bool) * n);
for(i = 2; i < upper_bound; i++)
if(A[i])
for(j = i*i; j < n; j += i)
A[j] = false;
for(i = 2; i < n; i++)
if(A[i])
printf("%d is prime\n", i);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment