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: Line 437:
=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>function Is_Prime(Item : Positive) return Boolean is
<lang ada>function Is_Prime(Item : Positive) return Boolean is
Result : Boolean := True;
Test : Natural;
Test : Natural;
begin
begin
if Item /= 2 and Item mod 2 = 0 then
if Item /= 2 and Item mod 2 = 0 then
Result := False;
return False;
else
else
Test := 3;
Test := 3;
while Test < Integer(Sqrt(Float(Item))) loop
while Test < Integer(Sqrt(Float(Item))) loop
if Item mod Test = 0 then
if Item mod Test = 0 then
Result := False;
return False;
exit;
end if;
end if;
Test := Test + 2;
Test := Test + 2;
end loop;
end loop;
end if;
end if;
return Result;
return True;
end Is_Prime;</lang>
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.
As an alternative, one can use the generic function Prime_Numbers.Is_Prime, as specified in [[Prime decomposition#Ada]], which also implements trial division.