Oberon-2

Oberon-2 is yet another Wirth programming language. One among many. It is intended to be a "lean and mean" version of Modula-2. Oberon compilers have been touted for their small size and efficiency.

This program compiles and runs under the Native XDS-x86 Personal Edition Modula-2/Oberon-2 Development Toolset.


<*+ MAIN *>


(*////////////////////////////////////////////////////////// *)


(*// Name: Peter M. Maurer *)


(*// Program: Sieve of Eratosthenes *)


(*// Due: Never *)


(*// Language: Oberon-2 *)


(*////////////////////////////////////////////////////////// *)


MODULE SieveEMod;


IMPORT STextIO,SWholeIO;



PROCEDURE SieveE*;


VAR


(* define the sieve data structure *)


Candidates : ARRAY 1000 OF INTEGER;


i : INTEGER;


j : INTEGER;


BEGIN


FOR i := 0 TO 999 DO


(* 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;


WHILE i<1000 DO


(* advance to the next un-crossed out number. *)


(* this number must be a prime *)


WHILE (i<1000) & (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 := 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 i := 0 TO 999 DO


IF Candidates[i] # 0 THEN


SWholeIO.WriteInt(i,3);


STextIO.WriteString(" is prime");


STextIO.WriteLn;


END


END


END SieveE;



BEGIN


SieveE;


END SieveEMod.

Click Here for the actual code.