Skip to content

Instantly share code, notes, and snippets.

@biwakonbu
Created April 30, 2012 18:02
Show Gist options
  • Save biwakonbu/2560516 to your computer and use it in GitHub Desktop.
Save biwakonbu/2560516 to your computer and use it in GitHub Desktop.
Eratosthenes
#include <stdio.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
int main (int argc, char* argv[])
{
int i, p, k, count, n;
char flag[1000000];
while (scanf("%d", &n) != EOF) {
if (2 <= n) {
count = 1;
} else {
count = 0;
}
for (i=0; i<=n; i++) {
flag[i] = TRUE;
}
for (i=0; i<=n; i++) {
if (flag[i]) {
p = i + i + 3;
printf("%8d\n", p);
for (k=i+p; k<=n; k+=p) {
flag[k] = FALSE;
}
if (p <= n) {
count++;
}
}
}
printf("number %d\n", count);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment