-- $mnt:[rob.ada]sieve.ada    rob   83-11-08
--
-- the infamous sieve of good ole Eratosthenes
--
-- done a reasonable way rather than the hacked up version in the 
-- Byte article (Jan 83) submitted by Telesoft
--
-- copied from listing of unknown origin
--------------------------------------------------------------
with text_io; use text_io;

procedure sieve is

 package number_io is new integer_io(integer);
 use number_io;

 limit        : constant natural := 20;
 number_found : natural := 0;
 prime_table  : array (2..limit) of boolean;
 multiple     : natural;

 begin
   for i in 2..limit
   loop
     prime_table(i) := true;
   end loop;
   for i in 2..limit  -- check the entire range
   loop
     if prime_table(i) then
       number_found := number_found + 1;
       put(i); put_line(" is prime");   -- print next prime
       multiple := i;
       loop
         multiple := multiple + i;
         exit when multiple > limit;
         prime_table(multiple) := false; -- remove all multiples
       end loop;
     end if;
   end loop;  -- for i
 
   put("there are ");
   put(number_found);
   put(" primes in the range 1 to ");
   put(limit);
   put_line(".");
 end sieve;