%////////////////////////////////////////////////////////// %// Name: Peter M. Maurer %// Program: Sieve of Eratosthenes %// Due: Never %// Language: CLU %////////////////////////////////////////////////////////// start_up = proc() po: stream := stream$primary_output() % define the sieve data structure % everything is potentially prime until proven otherwise Candidates := fill(0,1001,1) % Neither 1 nor 0 is prime, so flag them off Candidates[0] := 0 Candidates[1] := 0 % start the sieve with the integer 0 i:int := 0 while i<1000 do % advance to the next un-crossed out number. % this number must be a prime while i<1000 cand Candidates[i] = 0 do i := i + 1 end % insure against running off the end of the data structure if i<1000 then % cross out all multiples of the prime, starting with 2*p. j:int := 2 while j*i < 1000 do Candidates[j*i] := 0 j := j + 1 end % advance to the next candidate i := i + 1 end end % all uncrossed-out numbers are prime (and only those numbers) % print all primes for z:int in int$from_to(0, 999) do if Candidates[i] ~= 0 then stream$putl(po, int$unparse(z) || " is prime") end end end start_up