Created
July 28, 2012 07:39
-
-
Save darkf/3192274 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in C99
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 <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