home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / modula2 / library / modula1 / primes.mod < prev    next >
Text File  |  1987-06-11  |  1KB  |  41 lines

  1. (* Compute a table of the first n prime numbers.
  2.    Print m numbers per line. *)
  3.  
  4. MODULE primes;
  5.  
  6. FROM InOut IMPORT WriteLn, WriteCard;
  7.  
  8. CONST N = 500;
  9.       M = 23;  (* M ~ sqrt(N) *)
  10.       LL = 10; (* # of primes placed on a line *)
  11.  
  12. VAR i,k,x: CARDINAL;
  13.     inc,lim,square,L: CARDINAL;
  14.     prime: BOOLEAN;
  15.     P,V: ARRAY [0..M] OF CARDINAL;
  16.  
  17. BEGIN
  18.   L := 0; x := 1; inc := 4;
  19.   lim := 1; square := 9;
  20.   FOR i := 3 TO N DO
  21.     (* find next prime number p[i] *)
  22.     REPEAT
  23.       x := x+inc;
  24.       inc := 6-inc;
  25.       IF square <= x THEN
  26.         INC(lim); V[lim] := square;
  27.         square := P[lim+1] * P[lim+1]
  28.       END;
  29.       k := 2; prime := TRUE;
  30.       WHILE prime & (k < lim) DO
  31.         INC(k);
  32.         IF V[k] < x THEN V[k] := V[k] + 2*P[k] END;
  33.         prime := x # V[k]
  34.       END
  35.     UNTIL prime;
  36.     IF i <= M THEN P[i] := x END;
  37.     WriteCard(x,6); INC(L);
  38.     IF L = LL THEN WriteLn; L := 0 END
  39.   END
  40. END primes.
  41.