SieveE: PROCEDURE OPTIONS (MAIN); /* ///////////////////////////////////////////////////////// */ /* / Name: Peter M. Maurer */ /* / Program: Sieve of Eratosthenes */ /* / Due: Never */ /* / Language: PL/1 */ /* ///////////////////////////////////////////////////////// */ DECLARE Candidates(0:999) FIXED BIN(15); DECLARE i FIXED BIN(31); DECLARE j FIXED BIN(31); /* define the sieve data structure */ DO i=0 TO 999 BY 1; /* everything is potentially prime until proven otherwise */ Candidates(i) = 1; END; /* 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; DO WHILE (i<1000); /* advance to the next un-crossed out number. */ /* this number must be a prime */ DO WHILE ((i<1000) & (Candidates(i) = 0)); i = i + 1; END; /* insure against running off the end of the data structure */ IF (i<1000) THEN DO; /* cross out all multiples of the prime, starting with 2*p. */ DO j=2 TO 999 BY 1 WHILE (j*i < 1000); Candidates(j*i) = 0; END; /* advance to the next candidate */ i = i + 1; END; END; /* all uncrossed-out numbers are prime (and only those numbers) */ /* print all primes */ DO i=0 TO 999 BY 1; IF (Candidates(i) ^= 0) THEN DO; PUT SKIP LIST(i," is prime"); END; END; END SieveE; 