home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / compilers / p2 / tst / primes.pcat < prev    next >
Text File  |  2005-10-11  |  599b  |  33 lines

  1. (* This program prints out the first few primes using the
  2.    "Sieve of Eratosthenes" algorithm. *)
  3.  
  4. program is
  5.  
  6.   type A is array of boolean;
  7.   var N: integer := 0;
  8.       a: A := nil;
  9.       i,j: integer := 0;
  10.  
  11. begin
  12.  
  13.   write ("Printing the primes from 1 to N;  Please enter N...");
  14.   loop
  15.     read (N);
  16.     if N>=1 then exit; end;
  17.     write ("N must be >= 1.  Try again...");
  18.   end;
  19.   a := A { N+1 of true };
  20.   a[1] := false;
  21.   for i := 2 to N div 2 do
  22.     for j := 2 to N div i do
  23.       a [i*j] := false;
  24.     end;
  25.   end;
  26.   for i := 1 to N do
  27.     if a[i] then
  28.       write (i);
  29.     end;
  30.   end;
  31.  
  32. end;
  33.