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}

.



Task

Calculate all Mersenne primes up to the implementation's
maximum precision, or the 47th Mersenne prime   (whichever comes first).




*        Lucas-Lehmer test
LUCASLEH CSECT  
         USING  LUCASLEH,R12
SAVEARA  B      STM-SAVEARA(R15)
         DC     17F'0'
         DC     CL8'LUCASLEH'
STM      STM    R14,R12,12(R13) save calling context
         ST     R13,4(R15)      
         ST     R15,8(R13)
         LR     R12,R15         set addessability
*        ----   CODE
         LA     R2,2            R2=2
         LA     R11,0           R11:N'
         BCTR   R11,0           N':=X'FFFFFFFF'
         LA     R10,1           R10:N N=1
         LA     R4,1            R4:IEXP
         LA     R6,1            step 
         LH     R7,IEXPMAX      R7:IEXPMAX limit
LOOPE    BXH    R4,R6,ENDLOOPE	do iexp=2 to iexpmax
         SR     R3,R3           R3:S S=0 
         CR     R4,R2           if iexp=2 then S=0
         BE     OKS
         LA     R3,4            else S=4
OKS      EQU    *
         SLDA   R10,1           n=(n+1)*2-1
         LA     R5,0            I
         LA     R8,1            step 
         LR     R9,R4           IEXP
         SR     R9,R2           IEXP-2 limit
LOOPI    BXH    R5,R8,ENDLOOPI	do i=1 to iexp-2	 
*        ----   compute s=(s*s-2) MOD n 
         SR     R14,R14         R14=0
         LR     R15,R3          R15=S
         MR     R14,R3          R{14-15}=S*S
         SLR    R15,R2          R15=R15-2=S*S-2
         BNM    *+6             skip next if no borrow
         BCTR   R14,0           perform borrow
         DR     R14,R10         R10=N 
         LR     R3,R14          R14=MOD
         B      LOOPI
ENDLOOPI EQU    *
         LTR    R3,R3
         BNZ    NOPRT           if s<>0 then no print
         CVD    R4,P            store to packed P
         UNPK   Z,P             Z=P
         MVC    C,Z             C=Z
         OI     C+L'C-1,X'F0'   zap sign
         MVC    WTOBUF(4),C+12
         MVI    WTOBUF,C'M'
         WTO    MF=(E,WTOMSG)		  
NOPRT    EQU    *
         B      LOOPE
ENDLOOPE EQU    *
*        ----   END CODE
RETURN   EQU    *
         LM     R14,R12,12(R13)
         XR     R15,R15
         BR     R14
*        ----   DATA
IEXPMAX  DC     H'31'
I        DS     H
IEXP     DS     H
S        DS     F
N        DS     F
P        DS     PL8             packed
Z        DS     ZL16            zoned
C        DS     CL16            character 
WTOMSG   DS     0F
         DC     H'80',XL2'0000'
WTOBUF   DC     80C' '
         LTORG  
         YREGS  
         END    LUCASLEH