PROGRAM PRIMELIST1;
CONST size = 1000;
(* This is the maximum number of primes to be found, *)
(* and can be adjusted as required *)
VAR
n: extended;
i,j,no_primes: integer;
p: array[1..size] of extended;
pass: boolean;
BEGIN
write ('Number of primes = ? ')
readln (no_primes); (* This is < size *)
p[1]:= 2;
p[2]:= 3;
writeln (2,',',3); n:=3;
FOR i:= 3 TO no_primes DO
BEGIN
pass:= false;
WHILE NOT(pass) DO
(* So long as a new prime is not *)
(* discovered the program *)
(* adds 2 to n and tries again *)
BEGIN
n:= n + 2;
j:= 2;
(* Start by trial-dividing by the 2nd prime, 3 *)
pass:= true;
WHILE (pass AND (j < i) AND (p[ j ]*p[ j ] <=n)) DO
BEGIN
pass:= NOT(n = p[ j ] * INT(n/p[ j ] ));
(* The number n passes this test if it is not *)
(* divisble by the jth prime *)
j:= j + 1
END;
IF pass THEN
(* If n passes all the tests it is added to p *)
BEGIN
p[ i ]:= n;
write(',',n:0:0);
(* and also written to the screen *)
END;
END;
(* If NOT(pass) then 2 is added to n and we
(try again; *)
(* If pass then we go on to the next value of i *)
END;
END.
|