-------------------------------------------------------------
-- Prime_List
-- Generate a list of prime numbers, the count of which is
-- entered from the command line.
-------------------------------------------------------------
with Ada.Command_Line;
use Ada.Command_Line;
with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Integer_Text_Io;
use Ada.Integer_Text_Io;
with Ada.Numerics.Generic_Elementary_Functions;
procedure Prime_List is
subtype Max_Range is Positive range 2..Positive'Last;
function Is_Prime (Value : Positive ) return Boolean is
package Math is new Ada.Numerics.Generic_Elementary_Functions(Float);
use Math;
Num : Positive := 3;
Result : Boolean := True;
Limit : constant Positive := Positive(sqrt(Float(Value)));
begin
loop
if ((Value mod Num) = 0) and then Value /= Num then
Result := False;
exit;
end if;
exit when Num > Limit;
Num := Num + 2;
end loop;
return Result;
end Is_Prime;
procedure Print (Count : Natural; Item : Positive ) is
begin
Put(Item => Count, Width => 7);
Put(" : ");
Put(Item => Item, Width => 10);
New_Line;
end Print;
P1 : Positive := 2;
Possible : Positive;
Count : Natural := 1;
Max : Max_Range;
begin
Print(Count, P1);
Possible := 3;
Max := Max_Range'Value(Argument(1));
while Count < Max loop
if Is_Prime(Possible) then
Count := Count + 1;
Print(Count, Possible);
end if;
Possible := Possible + 2;
end loop;
end Prime_List;