Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. (*
  2.          adapted to Oberon-07 by 0CodErr, KolibriOS team
  3.                                                                 *)
  4.  
  5. (*    This was taken from the CRITICAL MASS MODULA-3 examples   *)
  6.  
  7. (* The "Sieve" program demonstrates the use of arrays,
  8.    loops and conditionals. *)
  9.  
  10. MODULE Sieve;
  11.  
  12. IMPORT In, Out, Console;
  13.  
  14. (* Search in interval 2 to 1000 for prime numbers. *)
  15. CONST
  16.     LastNum = 1000;
  17.  
  18. (* "prime" is an array of booleans ranging from 2 to "LastNum". *)
  19. VAR
  20.     prime: ARRAY LastNum + 2 OF BOOLEAN;
  21.     i, j: INTEGER;
  22.  
  23. BEGIN
  24.     Console.open;
  25.  
  26.     Out.String("Primes in range 2.."); Out.Int(LastNum, 1); Out.Char(":"); Out.Ln;
  27. (* Initialize all elements of the array to "TRUE".
  28.    (Note that we could have initialized the array during
  29.     the assignment.) *)
  30.     FOR i := 2 TO LastNum DO
  31.         prime[i] := TRUE
  32.     END;
  33. (* Loop through all integers between 2 and "LastNum".  Print each prime
  34.    number, starting from 2 and mark all numbers that are divisible by
  35.    that prime number to "FALSE". Repeat the step until we've exhausted
  36.    all the numbers in the interval.*)
  37.     FOR i := 2 TO LastNum DO
  38.         IF prime[i] THEN
  39.             Out.Int(i, 3);
  40.             Out.Char(" ");
  41.             FOR j := i TO LastNum DO
  42.                 IF j MOD i = 0 THEN
  43.                     prime[j] := FALSE
  44.                 END
  45.             END
  46.         END
  47.     END;
  48.     Out.Ln; In.Ln;
  49.  
  50.     Console.exit(TRUE)
  51. END Sieve.