File : sieve.sp


$ spar sieve
 2
 3
 5
 7
 11
 13
 17
 19


#!/usr/local/bin/spar

pragma annotate( summary, "sieve" );
pragma annotate( description, "The Sieve of Eratosthenes is a simple algorithm that" );
pragma annotate( description, "finds the prime numbers up to a given integer. Implement ");
pragma annotate( description, "this algorithm, with the only allowed optimization that" );
pragma annotate( description, "the outer loop can stop at the square root of the limit," );
pragma annotate( description, "and the inner loop may start at the square of the prime" );
pragma annotate( description, "just found. That means especially that you shouldn't" );
pragma annotate( description, "optimize by using pre-computed wheels, i.e. don't assume" );
pragma annotate( description, "you need only to cross out odd numbers (wheel based on" );
pragma annotate( description, "2), numbers equal to 1 or 5 modulo 6 (wheel based on 2" );
pragma annotate( description, "and 3), or similar wheels based on low primes." );
pragma annotate( see_also, "http://rosettacode.org/wiki/Sieve_of_Eratosthenes" );
pragma annotate( author, "Ken O. Burtch" );
pragma license( unrestricted );

pragma restriction( no_external_commands );

procedure sieve is 
   last_bool : constant positive := 20;
   type bool_array is array(2..last_bool) of boolean;
   a : bool_array;
 
   test_num : positive;  
   -- limit    : positive := positive(numerics.sqrt(float(arrays.last(a))));

   -- n : positive := 2;  
begin
   for i in arrays.first(a)..last_bool loop
     a(i) := true;
   end loop;

   for num in arrays.first(a)..last_bool loop
     if a(num) then
        test_num := num * num;
        while test_num <= last_bool loop
          a(test_num) := false;
          test_num := @ + num;
        end loop;
     end if;
   end loop;
 
   for i in arrays.first(a)..last_bool loop
     if a(i) then
       put_line(i);
     end if;
   end loop;
end sieve;

-- VIM editor formatting instructions
-- vim: ft=spar