I would like to plot the number of Mersenne primes (primes of the form $2^n-1$) lower than a given input. I am totally noob to mathematica and I do not know where to start from. I have tried to implement "Range[ ]" and "Length@" functions, but I do not really know how to properly use then. Thank you

Edit: I am trying this code:

MersennePrimes[n_] := With[{primes = Prime[Range[PrimePi[n]]]}, Length@Pick[primes, MersennePrimeExponentQ[n]]];

But i get this error:

Length::argx: Length called with 0 arguments; 1 argument is expected.

Any help?

  • $\begingroup$ Well, the documentation is generally a good place to start . Both reference.wolfram.com/language/ref/ListPlot.html and reference.wolfram.com/language/ref/MersennePrimeExponent.html may be useful for what you're trying to do. $\endgroup$
    – ktm
    Commented Dec 5, 2016 at 21:35
  • $\begingroup$ While this is very easy to accomplish in Mathematica, before anyone takes the time to construct something for you, you should post some of what you've tried so far. Knowing which part of the process you're struggling with will help us better address the issues you've ran into. $\endgroup$
    – ktm
    Commented Dec 5, 2016 at 21:36
  • $\begingroup$ @user6014 Thank you! Main post edited $\endgroup$ Commented Dec 5, 2016 at 21:48
  • 3
    $\begingroup$ @user3141592 I understand that you don't know how to use those functions, but if I were you I would try to read the manual and see if you can figure those out. You might also be interested in PrimeQ, or in MersennePrimeExponentQ from the documentation. $\endgroup$
    – MarcoB
    Commented Dec 5, 2016 at 21:56
  • 3
    $\begingroup$ Here's a hint for a simple implementation that counts the number of Mersenne primes less than a given number. You can use Count where the first argument to Count is Range[ ...something... ] and the second argument uses MersennePrimeExponentQ. $\endgroup$
    – Greg Hurst
    Commented Dec 5, 2016 at 22:02

1 Answer 1



There are $44$ Mersenne primes implemented in Mathematica. Their exponents are instantly returned with (the few more tentative ones can be added to this list manually)

exp = MersennePrimeExponent @ Range @ 44

{2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787, 1398269, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583, 25964951, 30402457, 32582657}

Mersenne primes are of the form $2^n-1$; one could compute them all with 2^exp-1, but the 44th Mersenne prime has nearly 10 million digits, so I don't recommend it (MMA can handle their display, though). Nevertheless, because they are of the form $2^n-1$, which is a monotonic function, it is sufficient to translate x - i.e., the input smaller of which you want to find the number of primes - to Log2[x + 1].


One can make a function:

f[x_] := Count[exp, u_ /; u < Log2[x + 1]]
















and for example




plot1 = LogLinearPlot[f[x], {x, 1, 10^20},  Frame -> True, PlotRange -> Full];
plot2 = LogLinearPlot[f[x], {x, 1, 10^300}, Frame -> True, PlotRange -> Full];
GraphicsRow[{plot1, plot2}, ImageSize -> 800]

enter image description here

The horizontal axis displays the exponent for the prime in Log10 basis. MMA is not able to go higher than the range showed in plot2. Note that, as mentioned,

Log10[2^Last[N @ exp]]


meaning that the 44th Mersenne prime has almost 10 million digits, being ridiculously big.


Your Answer

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

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