Lucas-Lehmer test: Difference between revisions

Content added Content deleted
m (tidy declarations/copy edit.)
(Ada example)
Line 4: Line 4:


The following programs calculate all Mersenne primes up to the implementation precision.
The following programs calculate all Mersenne primes up to the implementation precision.

=={{header|Ada}}==
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
procedure Lucas_Lehmer_Test is
function Mersenne(Item : Integer) return Boolean is
S : Long_Long_Integer := 4;
MP : Long_Long_Integer := 2**Item - 1;
begin
if Item = 2 then
return True;
else
for I in 3..Item loop
S := (S * S - 2) mod MP;
end loop;
return S = 0;
end if;
end Mersenne;
Upper_Bound : constant Integer := Integer(Float'Rounding(Log(10.0) / Log(2.0) * 10_000_000.0));
M_Count : Natural := 0;
begin
Put_Line(" Mersenne primes:");
for P in 2..Upper_Bound loop
if Mersenne(P) then
Put(" M");
Put(Item => P, Width => 1);
M_Count := M_Count + 1;
exit when M_Count = 45;
end if;
end loop;
end Lucas_Lehmer_Test;
Output: (Output was truncated by an arithmetic overflow exception.)
Mersenne primes:
M2 M3 M5 M7 M13 M17 M19 M31

=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Interpretor: algol68g-mk11
Interpretor: algol68g-mk11