user
Lucas-Lehmer Test:

for 

p
{\displaystyle {\displaystyle p}}
 an odd prime, the Mersenne number 

2

p
−
1
{\displaystyle {\displaystyle 2^{p}-1}}
 is prime if and only if 

2

p
−
1
{\displaystyle {\displaystyle 2^{p}-1}}
 divides 

S
(
p
−
1
)
{\displaystyle {\displaystyle S(p-1)}}
 where 

S
(
n
+
1
)
=
(
S
(
n
)

)

2
−
2
{\displaystyle {\displaystyle S(n+1)=(S(n))^{2}-2}}
, and 

S
(
1
)
=
4
{\displaystyle {\displaystyle S(1)=4}}
.

Task

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

```
multi is_mersenne_prime(2) { True }
multi is_mersenne_prime(Int $p) {
    my $m_p = 2 ** $p - 1;
    my $s = 4;
    $s = $s.expmod(2, $m_p) - 2 for 3 .. $p;
    !$s
}

.say for (2,3,5,7 … *).hyper(:8degree).grep( *.is-prime ).map: { next unless .&is_mersenne_prime; "M$_" };

```

On my system:

Letting it run for about a minute...

```
M2
M3
M5
M7
M13
M17
M19
M31
M61
M89
M107
M127
M521
M607
M1279
M2203
M2281
M3217
M4253
M4423
M9689
M9941
M11213
^C

real	0m55.527s
user	6m47.106s
sys	0m0.404s
```

Lightly modernized for better parallelization and run on a Ryzen 7950X with 16 cores. Followed examples set by Wren and Phix and didn't do an exhaustive search for larger Mersennes, just verified that known ones indeed were. Pure Raku. No external libraries.

```
multi is_mersenne_prime(2) { True }
multi is_mersenne_prime(Int $p) {
    my $m_p = 1 +< $p - 1;
    my $s = 4;
    $s = $s.expmod(2, $m_p) - 2 for 3 .. $p;
    !$s
}

# Exhaustive search for Mersenne primes up to 4600 - about half a second
my $start = now;
.say for (flat 2, (1..2300).map(* * 2 + 1)).hyper.map: 
  { next unless .&is-prime && .&is_mersenne_prime; "M$_ {(now - $start).round(.001)} running total seconds" };

# Cheat and just verify Mersenne primes > 4600 
.say for (9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243, 110503).hyper(:1batch).map:
  { next unless .&is_mersenne_prime; "M$_ {(now - $start).round(.001)} running total seconds" };

```

On my system, about 82 seconds:

```
M2 0.017 running total seconds
M3 0.018 running total seconds
M5 0.018 running total seconds
M7 0.019 running total seconds
M13 0.019 running total seconds
M17 0.019 running total seconds
M19 0.019 running total seconds
M31 0.019 running total seconds
M61 0.02 running total seconds
M89 0.021 running total seconds
M107 0.022 running total seconds
M127 0.022 running total seconds
M521 0.029 running total seconds
M607 0.039 running total seconds
M1279 0.084 running total seconds
M2203 0.096 running total seconds
M2281 0.164 running total seconds
M3217 0.173 running total seconds
M4253 0.336 running total seconds
M4423 0.457 running total seconds
M9689 0.868 running total seconds
M9941 0.878 running total seconds
M11213 0.953 running total seconds
M19937 1.828 running total seconds
M21701 2.054 running total seconds
M23209 2.324 running total seconds
M44497 9.186 running total seconds
M86243 46.836 running total seconds
M110503 81.91 running total seconds
```