# Selecting Elements from Lists

A Mersenne prime is a prime number of the form $2^n−1$. Produce a list of those n, which are at most 1,000, for which $2^n − 1$ is prime

I am able to find the list of prime numbers:

Select[Table[m = 2^n - 1, {n, Range[1000]}], PrimeQ]

{3, 7, 31, 127, 8191, 131071, 524287, 2147483647...etc.


How can I make a list of their corresponding "n" values?

• make the table just n and the select critera PrimeQ[2^# - 1]& Apr 5, 2018 at 20:58
• Or use With[{list = Range[1000]}, Pick[list, PrimeQ[2^list - 1]]]. Apr 5, 2018 at 21:00

Just to remove this from "unanswered". All credit goes to the original commenters.

@george2079

Select[Table[n, {n, Range[1000]}], PrimeQ[2^# - 1] &]


@Henrik Schumacher

With[{list = Range[1000]}, Pick[list, PrimeQ[2^list - 1]]]


You can also use MersennePrimeExponentQ with Select or Pick:

Select[MersennePrimeExponentQ] @ Range[1000]


{2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607}

Pick[#, MersennePrimeExponentQ /@ #]& @ Range[1000]


{2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607}

Alternatively, you can use MersennePrimeExponent with While:

i = 1; lst = {};
While[(a = MersennePrimeExponent[i++]) < 1000, AppendTo[lst, a]];
lst


{2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607}

Also

i = 1; While[MersennePrimeExponent[++i] < 1000];
MersennePrimeExponent[Range[i - 1]]


{2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607}