Primality by trial division: Difference between revisions

Content added Content deleted
(Added solution for Action!)
(→‎{{header|Ada}}: "return" statement cleanly exits from loops, therefore no need for a Result variable)
Line 437:
=={{header|Ada}}==
<lang ada>function Is_Prime(Item : Positive) return Boolean is
Result : Boolean := True;
Test : Natural;
begin
if Item /= 2 and Item mod 2 = 0 then
Result :=return False;
else
Test := 3;
while Test < Integer(Sqrt(Float(Item))) loop
if Item mod Test = 0 then
Result :=return False;
exit;
end if;
Test := Test + 2;
end loop;
end if;
return ResultTrue;
end Is_Prime;</lang>
 
<code>Sqrt</code> is made visible by a with / use clause on <code>Ada.Numerics.Elementary_Functions</code>.
 
As an alternative, one can use the generic function Prime_Numbers.Is_Prime, as specified in [[Prime decomposition#Ada]], which also implements trial division.