-- primes using a user defined exception
with Text_io;
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);
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
if P mod D = 0 then -- P is divisible by D
raise COMPOSITE;
end if;
exit DIVISIONS when D*D>P;
-- If P is divisible by D>sqrt(P) then P is divisble
-- by a number that is less than D.
end loop DIVISIONS;
INT_IO.put(P);
exception
when COMPOSITE=>null;
when others=>raise;
end;
end loop;
end PRIMES;