How to resolve the algorithm Lucas-Lehmer test step by step in the Ada programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Lucas-Lehmer test step by step in the Ada programming language
Table of Contents
Problem Statement
Lucas-Lehmer Test: for
p
{\displaystyle p}
an odd prime, the Mersenne number
2
p
− 1
{\displaystyle 2^{p}-1}
is prime if and only if
2
p
− 1
{\displaystyle 2^{p}-1}
divides
S ( p − 1 )
{\displaystyle S(p-1)}
where
S ( n + 1 )
( S ( n )
)
2
− 2
{\displaystyle S(n+1)=(S(n))^{2}-2}
, and
S ( 1 )
4
{\displaystyle S(1)=4}
.
Calculate all Mersenne primes up to the implementation's maximum precision, or the 47th Mersenne prime (whichever comes first).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Lucas-Lehmer test step by step in the Ada programming language
Source code in the ada programming language
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
procedure Lucas_Lehmer_Test is
type Ull is mod 2**64;
function Mersenne(Item : Integer) return Boolean is
S : Ull := 4;
MP : Ull := 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 := 64;
begin
Put_Line(" Mersenne primes:");
for P in 2..Upper_Bound loop
if Mersenne(P) then
Put(" M");
Put(Item => P, Width => 1);
end if;
end loop;
end Lucas_Lehmer_Test;
You may also check:How to resolve the algorithm ABC problem step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the J programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the C++ programming language
You may also check:How to resolve the algorithm Word search step by step in the zkl programming language