with Text_io;
with INTEGER_SETS; -- see files intset*.ad?
procedure PRIMES is -- outputs prime numbers between 1 and 100
-- A prime is a number P that can not be divided evenly
-- by a number between 2 and P-1.
-- 1 is considered to prime.
N:constant integer:=100;-- Largest number to be considered
package INT_IO is new Text_IO.Integer_io(integer);
PS:INTEGER_SETS.integer_set:=empty; --INVARIANT: Always PS=primes in 1..P-1
begin
for P in 1..N loop -- For each possible prime in turn...
declare
COMPOSITE:exception; -- assume prime, exception COMPOSITE if not
begin
DIVISIONS:
for D in 2..P-1 loop -- See if D divides P
exit DIVISIONS when D*D>P;
-- If P is divisible by D and D*D>P then P/DPS) -- Only need to try to divide by primes
then
if P mod D = 0 then -- P is divisible by D
raise COMPOSITE;
end if;
end if;
end loop DIVISIONS;
INT_IO.put(P); INTEGER_SETS.put(P, INTO=>PS);
exception
when COMPOSITE=>null;
when others=>raise;
end;
end loop;
end PRIMES;