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




 
sorenson = require('sieve').primes  # Sorenson's extensible sieve from task: Extensible Prime Number Generator
 
# Test if 2^n-1 is a Mersenne prime.
# assumes that the argument p is prime.
#
isMersennePrime = (p) ->
    if p is 2 then yes
    else
        n = (1n << BigInt p) - 1n
        s = 4n
        s = (s*s - 2n) % n for _ in [1..p-2]
        s is 0n
 
primes = sorenson()
mersennes = []
while (p = primes.next().value) < 3000
    if isMersennePrime(p)
        mersennes.push p
 
console.log "Some Mersenne primes: #{"M" + String p for p in mersennes}"
 