long sieve_next(sieve *sp) { long out; int ix = 0; long val; if(sp->next > sp->nbits) return -1; out = S_VAL(sp, sp->next); #ifdef DEBUG fprintf(stderr, "Sieving %ld\n", out); #endif /* Sieve out all multiples of the current prime */ val = out; while(ix < sp->nbits) { val += out; ix = S_BIT(sp, val); if((val & 1) && ix < sp->nbits) { /* && S_ISSET(sp, ix)) { */ S_CLR(sp, ix); #ifdef DEBUG fprintf(stderr, "Crossing out %ld (bit %d)\n", val, ix); #endif } } /* Scan ahead to the next prime */ ++sp->next; while(sp->next < sp->nbits && !S_ISSET(sp, sp->next)) ++sp->next; return out; }
int conf_read(const char *name) { FILE *in = NULL; char line[1024]; char *p, *p2; int lineno = 0; struct symbol *sym; struct property *prop; struct expr *e; int i; if (name) { in = fopen(name, "r"); } else { const char **names = conf_confnames; while ((name = *names++)) { name = conf_expand_value(name); in = fopen(name, "r"); if (in) { printf("#\n" "# using defaults found in %s\n" "#\n", name); break; } } } if (!in) return 1; for_all_symbols(i, sym) { sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED; sym->flags &= ~SYMBOL_VALID; switch (sym->type) { case S_INT: case S_HEX: case S_STRING: if (S_VAL(sym->def)) free(S_VAL(sym->def)); default: S_VAL(sym->def) = NULL; S_TRI(sym->def) = no; ; } }
int main(int argc, char *argv[]) { sieve s; long pr, *p; int c, ix, cur = 0; if(argc < 2) { fprintf(stderr, "Usage: %s <width>\n", argv[0]); return 1; } c = atoi(argv[1]); if(c < 0) c = -c; fprintf(stderr, "%s: sieving to %d positions\n", argv[0], c); sieve_init(&s, 3, c); c = 0; while((pr = sieve_next(&s)) > 0) { ++c; } p = calloc(c, sizeof(long)); if(!p) { fprintf(stderr, "%s: out of memory after first half\n", argv[0]); sieve_clear(&s); exit(1); } fprintf(stderr, "%s: half done ... \n", argv[0]); for(ix = 0; ix < s.nbits; ix++) { if(S_ISSET(&s, ix)) { p[cur] = S_VAL(&s, ix); printf("%ld\n", p[cur]); ++cur; } } sieve_reset(&s, p[cur - 1]); fprintf(stderr, "%s: crossing off %d found primes ... \n", argv[0], cur); for(ix = 0; ix < cur; ix++) { sieve_cross(&s, p[ix]); if(!(ix % 1000)) fputc('.', stderr); } fputc('\n', stderr); free(p); fprintf(stderr, "%s: sieving again from %ld ... \n", argv[0], p[cur - 1]); c = 0; while((pr = sieve_next(&s)) > 0) { ++c; } fprintf(stderr, "%s: done!\n", argv[0]); for(ix = 0; ix < s.nbits; ix++) { if(S_ISSET(&s, ix)) { printf("%ld\n", S_VAL(&s, ix)); } } sieve_clear(&s); return 0; }