[ create a new paste ] login | about

Link: http://codepad.org/mgXkjRaB    [ raw code | output | fork ]

C, pasted on Jul 27:
#include <stdio.h>
#include <stdlib.h>
int main() {
  int i, n, j;
  int *x;
  printf("N = ? ");
  scanf("%d", &n);
  if ((x = malloc(sizeof(int) * n)) == NULL) {
    fprintf(stderr, "no memory.\n");
    exit(-1);
  }
  for (i = 0; i < n; i++)
    x[i] = 0;
  x[0] = x[1] = 1;
  for (i = 2; i < n; i++) {
    if (x[i] == 0) {
      printf("%d, ", i);
      for (j = i + i; j < n; j += i)
        x[j] = 1;
    }
  }
  free(x);
  return 0;
}
/* end */


Output:
1
2
3
no memory.
N = ? 
Exited: ExitFailure 255


Create a new paste based on this one


Comments: