2
$\begingroup$

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?

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

2 Answers 2

5
$\begingroup$

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]]]
$\endgroup$
4
$\begingroup$

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}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.