-- $Date: 2003/12/23 12:57:33 $
-- $Revision: 1.7 $
-- $Author: jcrocholl $

package body Primes is

   -- Tests if N is a prime number.
   function Prime
     (N : in Positive-- The number to test.
     return Boolean    -- True if and only if N is prime.
   is
      D : Positive := 2;
   begin
      if N = 2 then return True; end if;
      loop
         if N mod D = 0 then return False; end if;
         if N / D < D then return True; end if;
         D := D + 1;
      end loop;
   end Prime;

   -- Returns the smallest prime number that is greater or equal to N.
   function Next_Prime
     (N : in Positive-- Start searching at this number.
     return Positive   -- The smallest prime number >= N.
   is
      Result : Positive := N;
   begin
      if Result mod 2 = 0 then
         if Result = 2 then return 2; end if;
         Result := Result + 1;
      end if;
      loop
         exit when Prime(Result);
         Result := Result + 2;
      end loop;
      return Result;
   end Next_Prime;

end Primes;