Advertisement
pseudocreator

Erastoten sieve

Mar 18th, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MODULE erastoten;
  2. FROM InOut IMPORT WriteString, WriteLn, WriteCard;
  3. FROM FIO IMPORT File, Open, Close, RdCard, EOF;
  4. CONST
  5.    n = 1000;
  6.    n2 = 1000;
  7. TYPE
  8.     CL = SET OF [2..n];
  9.     INTR = [0..n2];
  10. VAR
  11.    primes : CL;
  12.    arr : ARRAY INTR OF CARDINAL;
  13.    f : File;
  14.    i : CARDINAL;
  15.  
  16. PROCEDURE ES(): CL;
  17. //functional PROCEDURE for erastoten sieve
  18. VAR
  19.    i, next : CARDINAL;
  20.    sieve, prime : CL;
  21. BEGIN
  22.      sieve := CL{2..n};
  23.      prime := CL{};
  24.      next := 2;
  25.      REPEAT
  26.            WHILE NOT (next IN sieve) DO
  27.                  INC(next)
  28.            END;
  29.            INCL(prime, next);
  30.            i := next;
  31.            WHILE i <= n DO
  32.              EXCL(sieve,i);
  33.              i := i + next
  34.            END
  35.      UNTIL sieve = CL{};
  36.      RETURN prime;
  37. END ES;
  38.  
  39. BEGIN
  40.      //Enter name of file!
  41.      f:= Open('name.txt');
  42.      i := 0;
  43.      EOF := FALSE;
  44.      WHILE NOT EOF DO
  45.          INC(i);
  46.          arr[i] := RdCard(f);
  47.      END;
  48.      Close(f);
  49.      primes := ES();
  50.      FOR i := 1 TO n2 DO
  51.        IF arr[i] IN primes THEN
  52.          WriteString('Number ');
  53.          WriteCard(arr[i],0);
  54.          WriteString(' from input list is a prime number!')
  55.          WriteLn
  56.        END
  57.      END;
  58. END erastoten.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement