|
|
DataMuseum.dkPresents historical artifacts from the history of: CP/M |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about CP/M Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - download
Length: 1664 (0x680)
Types: TextFile
Names: »PRIMES.PAS«
└─⟦49cd49952⟧ Bits:30009431 50004848
└─⟦0c326962c⟧
└─⟦this⟧ »PRIMES.PAS«
PROGRAM primes(input,output);
æComputes all (odd) primes up to a given limit,
using the "sieve of Eratosthenes" å
CONST
maxnumber = 32767;
maxhalf = 16383; æmaxnumber DIV 2å
TYPE
number = 1..maxnumber;
half = 1..maxhalf;
VAR
count,factor,maxfactor,limit,nonprime: number;
index,halflimit: half;
primeflags: ARRAY ÆhalfÅ OF boolean;
BEGIN
REPEAT
writeln('Upper limit = ?');
readln(limit);
UNTIL (1 <= limit) AND (limit <= maxnumber);
halflimit := limit DIV 2;
FOR index := 1 TO halflimit DO primeflagsÆindexÅ := true;
æIf a number is composite, at least one of its factors is
less than or equal to its square root, so: å
maxfactor := round(sqrt(limit));
count := 1; æcount of primes: 2 is the only even primeå
factor := 1; æfirst actual factor used will be 3å
FOR index := 1 TO halflimit DO
BEGIN
factor := factor + 2; æi.e. factor := 2*index + 1å
IF primeflagsÆindexÅ THEN
æA new prime has been found å
BEGIN count := succ(count);
æIf necessary, cross out all its multipleså
IF factor <= maxfactor THEN
BEGIN nonprime := index + factor;
WHILE nonprime <= halflimit DO
BEGIN
primeflagsÆnonprimeÅ := false;
nonprime := nonprime + factor;
END æWHILEå;
END;
æ writeln(factor); å æomitted for timingå
END æIFå;
END æFORå;
writeln('Number of primes up to ',limit,' = ',count);
END.
«eof»