Sieve.ob07 1.4 KB

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