How to resolve the algorithm Lucas-Lehmer test step by step in the RPL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Lucas-Lehmer test step by step in the RPL 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 RPL programming language

Source code in the rpl programming language

%%HP: T(3)A(R)F(.);                                                          ; ASCII transfer header 
\<< DUP LN DUP \pi * 4 SWAP / 1 + UNROT / * IP 2 { 2 } ROT 2 SWAP            ; input n; n := Int(n/ln(n)*(1 + 4/(pi*ln(n)))), p:=2; (n ~ number of primes less then n, pi used here only as a convenience),  2 is assumed to be the 1st elemente in the list
  START SWAP NEXTPRIME DUP UNROT DUP 2 SWAP ^ 1 - 4 PICK3 2 - 1 SWAP         ; for i := 2 to n,  p := nextprime;  s := 4; m := 2^p - 1;
    START SQ 2 - OVER MOD                                                    ;   for j := 1 to p - 2;  s := s^2 mod m;  
    NEXT NIP NOT { + } { DROP } IFTE                                         ;   next j;  if s = 0 then add p to the list else discard p; 
  NEXT NIP                                                                   ; next i;     
\>>

  

You may also check:How to resolve the algorithm Enumerations step by step in the Scala programming language
You may also check:How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Bitmap/Flood fill step by step in the Go programming language
You may also check:How to resolve the algorithm ASCII art diagram converter step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the REXX programming language