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).




PROGRAM LL_TEST
 
!$DOUBLE
 
PROCEDURE LUCAS_LEHMER(P%->RES)
     LOCAL I%,MP,SN
     IF P%=2 THEN RES%=TRUE EXIT PROCEDURE END IF
     IF (P% AND 1)=0 THEN RES%=FALSE EXIT PROCEDURE END IF
     MP=2^P%-1
     SN=4
     FOR I%=3 TO P% DO
        SN=SN^2-2
        SN-=(MP*INT(SN/MP))
     END FOR
     RES%=(SN=0)
END PROCEDURE
 
BEGIN
     PRINT("Mersenne Primes:")
     FOR P%=2 TO 23 DO
        LUCAS_LEHMER(P%->RES%)
        IF RES% THEN PRINT("M";P%) END IF
     END FOR
END PROGRAM
 