[ create a new paste ] login | about

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

C, pasted on Jun 11:
#include <stdio.h>
#include <stdlib.h> 
#define M 10
 
int g(int n, int s[], int r, int t) { return (t <= n) ? (s[t] = 1, g(n, s, r, t + r)) : 0; }
int h(int n, int s[], int r, int k) {
  return (r >= n) ? 0
                  : (s[r] == 0) ? ( printf("%-3d ", r),
                                    (k == M - 1) ? putchar('\n') : 0,
                                    g(n, s, r, r),
                                    h(n, s, r + 1, (k == M - 1) ? 0 : k + 1) )
                                : h(n, s, r + 1, k);
}
 
int f(int n) {
  int *s;
  return (s = calloc((n + 1), sizeof(int))) ? ( s[0] = s[1] = 1,
                                                 h(n, s, 2, 0),
                                                 free(s), 0)
                                              : 0;
}

#define DEFAULT_NUM 100
int main() {
  int n;
  do {
    printf("n = ");
    if (scanf("%d", &n) != 1)
      n = DEFAULT_NUM;
  } while (!(n >= 2));
  putchar('\n');
  f(n);
  return 0;
}
/* end */


Output:
1
2
3
4
n = 
2   3   5   7   11  13  17  19  23  29  
31  37  41  43  47  53  59  61  67  71  
73  79  83  89  97  


Create a new paste based on this one


Comments: