Ada 2012.
I have this definition :
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
package body Primes is
function is_prime(n : Integer) return Boolean
is
i : Integer := 5;
begin
if n <= 3 then
return n > 1;
elsif n mod 2 = 0 or else n mod 3 = 0 then
return false;
else
--for i in 5 .. Sqrt()n + 1
loop
if n mod i = 0 or else n mod (i + 2) = 0 then
return false;
end if;
exit when i >= Sqrt(n) + 1;
i := i + 6;
end loop;
return true;
end if;
end is_prime;
end Primes;
That checks whether n is prime or not. The this is line:
exit when i >= Sqrt(n) + 1;
Provides errors
expected type "Standard.Float"
found type "Standard.Integer"
I've been trying to Float'Value() on all parts of the statement, but then I got some error regarding what I think is exit when type (String). Now I'm stuck at this thing and can't get it to compile. Everything else is fine and doesn't need to be reviewed.
The reason why I used loop -> exit when -> end loop is because Ada doesn't support For loop with a specified step like for example Java:
for (int i = 5; i < Math.sqrt(n) + 1; i +=6)