/*//////////////////////////////////////////////////////// // Name: Peter M. Maurer // Program: Sieve of Eratosthenes // Due: Never // Language: B ////////////////////////////////////////////////////////*/ main() { /* define the sieve data structure */ auto Candidates[1000],i,j; i = 0; while (i<1000) { /* everything is potentially prime until proven otherwise*/ Candidates[i] = 1; i++; } /* Neither 1 nor 0 is prime, so flag them off*/ Candidates[0] = 0; Candidates[1] = 0; /* start the sieve with the integer 0*/ i = 0; while (i<1000) { /* advance to the next un-crossed out number.*/ /* this number must be a prime*/ while (i<1000 & Candidates[i] == 0) { i++; } /* insure against running off the end of the data structure*/ if (i<1000) { /* cross out all multiples of the prime, starting with 2*p.*/ j = 2; while (j*i < 1000) { Candidates[j*i] = 0; j++; } /* advance to the next candidate*/ i++; } } /* all uncrossed-out numbers are prime (and only those numbers)*/ /* print all primes*/ i = 0; while (i<1000) { if (Candidates[i] != 0) { putnumb(i); putchar(' is '); putchar('prim'); putchar('e*n'); } i++; } }