kolibrios/programs/develop/oberon07/Samples/Windows/Console/Sieve.ob07

51 lines
1.4 KiB
Plaintext
Raw Normal View History

(*
adapted to Oberon-07 by 0CodErr, KolibriOS team
*)
(* This was taken from the CRITICAL MASS MODULA-3 examples *)
(* The "Sieve" program demonstrates the use of arrays,
loops and conditionals. *)
MODULE Sieve;
IMPORT In, Out, Console;
(* Search in interval 2 to 1000 for prime numbers. *)
CONST
LastNum = 1000;
(* "prime" is an array of booleans ranging from 2 to "LastNum". *)
VAR
prime: ARRAY LastNum + 2 OF BOOLEAN;
i, j: INTEGER;
BEGIN
Console.open;
Out.String("Primes in range 2.."); Out.Int(LastNum, 1); Out.Char(":"); Out.Ln;
(* Initialize all elements of the array to "TRUE".
(Note that we could have initialized the array during
the assignment.) *)
FOR i := 2 TO LastNum DO
prime[i] := TRUE
END;
(* Loop through all integers between 2 and "LastNum". Print each prime
number, starting from 2 and mark all numbers that are divisible by
that prime number to "FALSE". Repeat the step until we've exhausted
all the numbers in the interval.*)
FOR i := 2 TO LastNum DO
IF prime[i] THEN
Out.Int(i, 3);
Out.Char(" ");
FOR j := i TO LastNum DO
IF j MOD i = 0 THEN
prime[j] := FALSE
END
END
END
END;
Out.Ln; In.Ln;
Console.exit(TRUE)
END Sieve.